\documentclass[12pt]{article}
\usepackage{times}
\usepackage{graphicx}
\usepackage{sidecap}
\usepackage{wrapfig}
\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\baselinestretch}{1.2}
\newenvironment{mylisting}
{\begin{list}{}{\setlength{\leftmargin}{1em}}\item\scriptsize\bfseries}
{\end{list}}
\newenvironment{mylisting2}
{\begin{list}{}{\setlength{\leftmargin}{1em}}\item\scriptsize\bfseries}
{\end{list}}

%%% This file is Open Content

\begin{document}
	\textbf{{\Huge Kobold's Quest 2}}
{\center{Dec 29, 2011, 
	Johan Ceuppens, Theo D'Hondt - Vrije Universiteit Brussel}}
\\
{\large{\textbf{\center Abstract}}}
\\
\\
\section{\large Introduction}



\begin{mylisting}
\begin{verbatim}

	

# Copyright (C) Johan Ceuppens 2011
# Copyright (C) Johan Ceuppens 2010

# Copyright (C) Johan Ceuppens 2009 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


class Block:
    "Block box"
    def __init__(self,x,y,z,w,h,d):
        self.x = x
        self.y = y
        self.z = z 
        self.w = w
        self.h = h
        self.d = d ## depth 
    
    #FIXME copy-paste entity
    def addtolist(self,list):
        list.addobject(self)

# Copyright (C) Johan Ceuppens 2011
# Copyright (C) Johan Ceuppens 2010

# Copyright (C) Johan Ceuppens 2009 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import pygame
from math import *
from engine import *
from enemytalking import *
from talksystembombman import *
from list import *

class Bombman(Enemytalking):
    "Bombman"
    def __init__(self,x,y,w,h, boxes, entities, player):
        Enemytalking.__init__(self,x,y,w,h,Talksystembombman())
        ###self.images = List()

	self.image = pygame.image.load("./pics/bombman1-333x333.bmp").convert()
        self.image.set_colorkey((0,0,0))
	
	self.addimage("./pics/bombman1-200x200.bmp",0,0,0)

    def draw3(self,color):
	i = 0
	j = 0
	n = 10
	cx = 100
	cy = 100
	PI = 3.14152829
	r = 100
	theta1 = PI/2 
	y = 0	
	while i < n:
		theta1 -= .01 
		xx = i
		xx *= sin(theta1)
		x = xx
		 
		pygame.draw.line(pygame.display.get_surface(),color, (cx+x*r,cy+y*r),(cx-x*r,cy+y*r))
		y += 1	
		i += .001

    def draw2(self,color):
	rotx = 0#sqrt(2)/2 
	roty = 0#sqrt(2)/2 
	rotz = 0#sqrt(2)/2 

	self.i = 0 ## / medridians, / paralells
	self.j = 0###-2*3.14 ## / medridians, / paralells
	self.PI = 3.14152829
	self.cx = 100
	self.cy = 100
	self.cz = 100
	self.n = 10
	self.r = 100
	# NOTE or enlarge radius r before 2 PI loops
	while rotx < 2*self.PI:
		rotx += .01
		roty += .01
		rotz += .01
		e = engine(rotx,roty,rotz)
		while self.j < self.n / 2:
			self.theta1 = self.j*2*self.PI / self.n - self.PI
			self.theta2 = (self.j+1)*2*self.PI / self.n - self.PI
			while self.i < self.n:
				self.theta3 = self.i * 2*self.PI / self.n
				self.x = cos(self.theta2)*cos(self.theta3) 
				self.y = sin(self.theta2)*sin(self.theta3)
				self.z = sin(self.theta2)
				r1 = Vector3(self.x,self.y,self.z)
				if rotx:
					r1 = e.mx.multiply(r1)
				if roty:
					r1 = e.my.multiply(r1)
				if rotz:
					r1 = e.mz.multiply(r1)
				self.x = r1.array[0]	
				self.y = r1.array[1]	
				self.z = r1.array[2]	
				### pygame.draw.line(pygame.display.get_surface(),color, (self.cx+r1x*self.r,self.cy+r1y*self.r),(self.cx+r1x*self.r,self.cy+r1y*self.r))
				pygame.draw.line(pygame.display.get_surface(),color, (self.cx+self.x,self.cy+self.y),(self.cx+self.x,self.cy+self.y))
				self.i += .001 
			self.j += .001


    def draw4(self,color):
	self.i = 0 ## / medridians, / paralells
	self.j = 0###-2*3.14 ## / medridians, / paralells
	self.PI = 3.14152829
	self.cx = 100
	self.cy = 100
	self.cz = 100
	self.n = 100
	self.r = 100
	while self.j < self.n / 2:
		self.theta1 = self.j*2*self.PI / self.n - self.PI
		self.theta2 = (self.j+1)*2*self.PI / self.n - self.PI
		while self.i < self.n:
			self.theta3 = self.i * 2*self.PI / self.n
			self.x = cos(self.theta2)*cos(self.theta3) 
			self.y = sin(self.theta2)*sin(self.theta3)
			self.z = sin(self.theta2)
			self.x *= self.r
			self.y *= self.r
			self.z *= self.r
			pygame.draw.line(pygame.display.get_surface(),color, (self.cx+self.x,self.cy+self.y),(self.cx+self.x,self.cy+self.y))
			self.i += .001 
		self.j += .001

    def drawonmap(self, screen, maproom):
        self.imagescounter += 1
        if self.imagescounter == self.maximages:
            self.imagescounter = 0     
###        screen.scr.blit(self.images[self.imagescounter], (self.x+maproom.relativex, self.y+maproom.relativey))
	self.room = maproom
	relx = self.room.roomman.room.relativex
	rely = self.room.roomman.room.relativey
       	pygame.display.get_surface().blit(self.images[0], (self.x+relx,self.y+rely))
  
    def draw(self,room):
	self.room = room
	relx = self.room.roomman.room.relativex
	rely = self.room.roomman.room.relativey
       	pygame.display.get_surface().blit(self.images.getwithindex(0), (self.x+relx,self.y+rely))


# Copyright (C) Johan Ceuppens 2011
# Copyright (C) Johan Ceuppens 2010

# Copyright (C) Johan Ceuppens 2009 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


class Box:
    "Box"
    def __init__(self,x,y,w,h):
        self.x = x
        self.y = y
        self.w = w
        self.h = h
    
    #FIXME copy-paste entity
    def addtolist(self,list):
        list.addobject(self)

# Copyright (C) Johan Ceuppens 2011
# Copyright (C) Johan Ceuppens 2010

# Copyright (C) Johan Ceuppens 2009 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


from enemytalking import *
from talksystemcapricornguardian3 import *
from entityfactory import *
from food import *

class Capricornguardian3(Enemytalking):
    def __init__(self,x,y,w,h, boxes, entities, player):
    ###def __init__(self,x,y,w,h):
        Enemytalking.__init__(self,x,y,w,h,Talksystemcapricornguardian3())
        self.addimage('./pics/capricornguardian1.bmp',0,0,0)
        self.addimage('./pics/capricornguardian1.bmp',0,0,0)
        self.addimage('./pics/capricornguardian1.bmp',0,0,0)
        self.addimage('./pics/capricornguardian1.bmp',0,0,0)
        self.addimage('./pics/capricornguardian2.bmp',0,0,0)
        self.addimage('./pics/capricornguardian2.bmp',0,0,0)
        self.addimage('./pics/capricornguardian2.bmp',0,0,0)
        self.addimage('./pics/capricornguardian2.bmp',0,0,0)
        self.addimage('./pics/capricornguardian3.bmp',0,0,0)
        self.addimage('./pics/capricornguardian3.bmp',0,0,0)
        self.addimage('./pics/capricornguardian3.bmp',0,0,0)
        self.addimage('./pics/capricornguardian3.bmp',0,0,0)
        self.addimage('./pics/capricornguardian2.bmp',0,0,0)
        self.addimage('./pics/capricornguardian2.bmp',0,0,0)
        self.addimage('./pics/capricornguardian2.bmp',0,0,0)
        self.addimage('./pics/capricornguardian2.bmp',0,0,0)

	self.images2 = []
        self.addimage2('./pics/capricornguardianup1.bmp',0,0,0)
        self.addimage2('./pics/capricornguardianup1.bmp',0,0,0)
        self.addimage2('./pics/capricornguardianup1.bmp',0,0,0)
        self.addimage2('./pics/capricornguardianup1.bmp',0,0,0)
        self.addimage2('./pics/capricornguardianup2.bmp',0,0,0)
        self.addimage2('./pics/capricornguardianup2.bmp',0,0,0)
        self.addimage2('./pics/capricornguardianup2.bmp',0,0,0)
        self.addimage2('./pics/capricornguardianup2.bmp',0,0,0)
        self.addimage2('./pics/capricornguardianup3.bmp',0,0,0)
        self.addimage2('./pics/capricornguardianup3.bmp',0,0,0)
        self.addimage2('./pics/capricornguardianup3.bmp',0,0,0)
        self.addimage2('./pics/capricornguardianup3.bmp',0,0,0)
        self.addimage2('./pics/capricornguardianup2.bmp',0,0,0)
        self.addimage2('./pics/capricornguardianup2.bmp',0,0,0)
        self.addimage2('./pics/capricornguardianup2.bmp',0,0,0)
        self.addimage2('./pics/capricornguardianup2.bmp',0,0,0)

        self.counter = 0
	self.hitpoints = 10
	self.maxhitpoints = 10
	self.direction = "down"
	self.boxes = boxes
	self.entities = entities 
	self.player = player

####### TEXT / FONT interface
    def getname(self,text):
        return text.getcapricornguardianname()

#### NEW FUNCTION
    def addimage2(self,filename,r,g,b):# rgb transparency values
### NOTE same number of images already        self.maximages2 += 1

        image2 = pygame.image.load(filename).convert()
        image2.set_colorkey((r,g,b))
        self.images2.append(image2)

    def collisionboxesnew(self,room):
        o = self.collisionlist(room.boxes)
        if o:
            #self.bounceback()#FIXME1 level arg
            return 1
        return None

    def update(self,room):
##	if self.entangled:
		return
###	if self.hitpoints < self.maxhitpoints:
###		room	
###		return

####### other methods
    def drawonmap(self, screen, maproom):
	if self.direction == "down": 
        	screen.scr.blit(self.images[0], (self.x+maproom.relativex, self.y+maproom.relativey))
	elif self.direction == "up": 
        	screen.scr.blit(self.images2[0], (self.x+maproom.relativex, self.y+maproom.relativey))

    def draw(self,screen):
        self.counter += 1

        self.imagescounter += 1
	if self.direction == "down": 
        	screen.scr.blit(self.images[0], (self.x, self.y))
	elif self.direction == "up": 
        	screen.scr.blit(self.images2[0], (self.x, self.y))
#	if self.entangled:
#        	rootsimage = pygame.image.load('entangleroots1.bmp').convert()
#        	rootsimage.set_colorkey((0,0,0))
#        	screen.scr.blit(rootsimage, (self.x, self.y+self.h-40))
        

#     def leftbuttonpressed(self,mouse,text):
#         return self.talksystem.gettalk(text)#foo

    def remove(self,list):
	### WITH PLAYER COLLISION ON o YOU RESHOOT
###	list.add(LightningProbeBullet(self.x,self.y,50,50,self.boxes,self.player))

        if self.hitpoints <= 0:
            list.remove(self)

	
    def leftbuttonpressedwithinventoryitem(self,game):
        if Enemytalking.leftbuttonpressedwithinventoryitem(self,game):
            print "You use the item on the knight."
            return 1
        else:
            return None



    def collisionboxes(self):
        o = self.collisionlist(self.boxes)
        
        if o:
            #player.bounceback()#FIXME1 level arg
            return 1
        return None

    def bounceback(self):
        if self.direction == "up":#north
	    self.y += 12 
	   # self.direction = 3 
        elif self.direction == "down":#south
	    self.y -= 12
	   # self.direction = 1 
        elif self.direction == "left":#west
	    self.x -= 12
	   # self.direction = 2 
        elif self.direction == "right":#east
	    self.x += 12
	   # self.direction = 4 
	# set direction
	#r = randint(1,4)
	#self.direction = r

    def collisionlist(self,list):
        return self.collisionlist1(self,list)

    # GEN PROC
    def collisionlist1(self,collider,list):
        for i in range(0,list.length):
            o = None
            o = list.getlistobject(o)
            
            if o:
#                print 'i=%d x=%d y=%d px=%d py=%d' % (i, o.x,o.y,collider.x,collider.y)
                if collider.x >= o.x and collider.x <= o.x + o.w and collider.y >= o.y and collider.y <= o.y + o.h :   	
                    return o
        return None

    def donotslay(self, player):
	if self.hitpoints < self.maxhitpoints:
		r = randint(0,1000)
		if r >= 995:
			self.entities.add(LightningProbeBullet(self.x,self.y,50,50,self.boxes,self.player))
		player.donotslay()

    def usegoldcoin(self,game):
        string = "You have used the gold coin"
        string += "\n"

	s = ""

        f = open("./workfile",'r')
        for line in f:
            s += line
                
        f = open("./workfile",'w')
        
        f.write(s)
        f.write(string)
        f.write("\n")

	self.talksystem = Talksystemangelspellbookdestroy()
	self.usedspellbookdracolich = 1
	coin = Goldcoin(300,400,80,80,"1","1")
	game.level.roommanager.room.entities.add(coin)
	### coin.remove(game.level.roommanager.room.entities)

	game.level.roommanager.room.entities.remove(self)
        return game.text.getcapricornguardiandisappear()

    def hitcommand(self, player,type, value, damagevalue):
        player.hitcommand(player, type, value, damagevalue)	

# Copyright (C) Johan Ceuppens 2011
# Copyright (C) Johan Ceuppens 2010

# Copyright (C) Johan Ceuppens 2009 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


from entity import *

class Enemy(Entity):
    def __init__(self,x,y,w,h):
        Entity.__init__(self,x,y,w,h)

        self.decreasehp = 1
        self.hitpoints = 1

        self.direction = "north"

    def hit(self):
        r = randint(0,20)
        if r >= 1:
            print "hit"
            self.hitpoints -= self.decreasehp
        else:
            print "miss"
        
    def remove(self,list):
        if self.hitpoints <= 0:
            list.remove(self)

    def bounceback(self):
        if self.direction == "north":
            self.y += 2 
            #FIXME self.direction = "south"
        elif self.direction == "south":
            self.y -= 2
            #self.moveup()
        elif self.direction == "west":
            self.x += 2
            #self.moveright()
        elif self.direction == "east":
            self.x -= 2
            #self.moveleft()


    def decreasehpinplayer(self, player, level):
	player.decreasehp(level)	
            

# Copyright (C) Johan Ceuppens 2011
# Copyright (C) Johan Ceuppens 2010

# Copyright (C) Johan Ceuppens 2009 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


from enemy import *

class Enemytalking(Enemy):
    def __init__(self,x,y,w,h,talksystem):
        Enemy.__init__(self,x,y,w,h)
        self.talksystem = talksystem
        self.talkcounter = 0
        self.currenttext = ""

    def leftbuttonpressed(self,game):#mouse,text,press0):
        if game.mouse.press(0) == 1:
            self.talkcounter += 1
            if self.talkcounter == 1:
                self.currenttext = self.talksystem.gettalk(game.text)#foo
            return self.currenttext
        else:
            self.talkcounter = 0 	
            return self.currenttext

    def leftbuttonpressedwithinventoryitem(self,game):
        if game.mouse.press(0) == 1:
            self.talkcounter += 1
		### If there is an inventory item has been chosen, return string
            if game.inventoryitem:
                return game.inventoryitem.use(self,game)
            return 1
        else:
            self.talkcounter = 0 	
            return None#self.currenttext

    def remove(self,list):
        if self.hitpoints <= 0:
            list.remove(self)    
import pygame, sys,os
from pygame.locals import *
from math import *
from vector3 import *
from matrix3 import *
import math

class engine():
    def __init__(self,theta1,theta2,theta3):
	###theta1 = self.theta1 = sqrt(2)/2 
	###theta2 = self.theta2 = sqrt(2)/2 
	###theta3 = self.theta3 = sqrt(2)/2 
	self.mx = Matrix3(1,0,0,
			0,math.cos(theta1),-math.sin(theta1),
			0,math.sin(theta1),math.cos(theta1))
	self.my = Matrix3(math.cos(theta2)/1.0,0, math.sin(theta2)/1.0,
			0,1,0,
			-math.sin(theta2),0, math.cos(theta2))
	self.mz = Matrix3(math.cos(theta3)/1.0, -math.sin(theta3)/1.0, 0,
			math.sin(theta3)/1.0, math.cos(theta3)/1.0, 0,
			0,0,1)


    def rotate(self, t1,t2,t3):
	self.mx = Matrix3(1,0,0,
			0,math.cos(t1),-math.sin(t1),
			0,math.sin(t1),math.cos(t1))
	self.my = Matrix3(math.cos(t2)/1.0,0, math.sin(t2)/1.0,
			0,1,0,
			-math.sin(t2),0, math.cos(t2))
	self.mz = Matrix3(math.cos(t3)/1.0, -math.sin(t3)/1.0,0.0,
			math.sin(t3)/1.0, math.cos(t3)/1.0, 0.0,
			0.0,0.0,1.0)

#    def drawLine(self, x1,y1,x2,y2):
#	xlen = x2-x1
#	ylen = y2-y1
#	xmod = xlen / (ylen + 1)#Float division with 0
#	ymod = ylen / (xlen + 1)
#	yy = y1
#	if x1<x2:
#	    xx = x1
#	    while xx < x2 or yy < y2:
#	        self.screen.blit(self.surf, (xx, yy))
#	        xx += xmod
#	        yy += ymod
#	else:
#	    xx = x2
#	    while xx < x1 or yy < y2:
#	        self.screen.blit(self.surf, (xx, yy))
#	        xx += xmod
#	        yy += ymod
	    

    def display(self, v1, v2):
	m = self.mx.multiplyByMatrix(self.my)
	m = m.multiplyByMatrix(self.mz)
	retv1 = m.multiply(v1)
	retv2 = m.multiply(v2)
	pygame.draw.line(self.screen, (255,255,255),
			(retv1.array[0],retv1.array[1]),
			(retv2.array[0],retv2.array[1]))
	return retv1

# Copyright (C) Johan Ceuppens 2011
# Copyright (C) Johan Ceuppens 2010

# Copyright (C) Johan Ceuppens 2009 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from myconid import *
from food import *
from capricornguardian3 import *
from bombman import *

class Entityfactory(object):
    
    def makewith(self, entityType,x,y,w,h, boxes, entities, player):
        classByType = {
            Entity.ENTITY_CAPRICORNGUARDIAN3 : Capricornguardian3,
            Entity.ENTITY_BOMBMAN1 : Bombman,
        }
        return classByType[entityType](x,y,w,h, boxes, entities,player)

    def make(self, entityType,x,y,w,h):
        classByType = {
            Entity.ENTITY_MYCONID : Myconid,
            Entity.ENTITY_FOOD : Food,
        }
        return classByType[entityType](x,y,w,h)

    def add(self,room):
        room.entityfactory = self


# Copyright (C) Johan Ceuppens 2011
# Copyright (C) Johan Ceuppens 2010

# Copyright (C) Johan Ceuppens 2009 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import pygame
from pygame.locals import *

from random import *
import os
from utility import *

class Entity(object):


    ENTITY_CAPRICORNGUARDIAN3,ENTITY_BAT, ENTITY_SKELETON,ENTITY_SKELETON2, ENTITY_SKELETONUNDERPLAYERCOMMAND, ENTITY_TREE2,ENTITY_TREE3,ENTITY_TREE4,ENTITY_TREE5,ENTITY_ENT,ENTITY_MYCONID,ENTITY_MYCONID_RED,ENTITY_EARTHWEIRD,ENTITY_KOBOLDMAGE,ENTITY_ORCPALADIN,ENTITY_ANGEL,ENTITY_HOBGOBLINSWORDSMAN1,ENTITY_DEMON1,ENTITY_HOBGOBLINSWORDSMANCHIEF1,ENTITY_PLATFORM1,ENTITY_PLATFORMMAPROOM1,ENTITY_CHERUBSTATUE1,ENTITY_FOOD,ENTITY_BATCRAZY,ENTITY_BOUNCER1,ENTITY_KNIGHTDARK1,ENTITY_HOUNDARCHON1,ENTITY_OOZEBIG1,ENTITY_WATERDEVIL,ENTITY_WATERDEVILFEMALE,ENTITY_TREESTONE,ENTITY_ENSORCELLEDTREE,ENTITY_NUTTER1,ENTITY_SPARROW,ENTITY_NUTTER2,ENTITY_TROLLBLACK,ENTITY_UNICORNSTATUE,ENTITY_UNICORNSTATUELITTLE,ENTITY_HERMIT,ENTITY_CANDLEFLAME1,ENTITY_CANDLEFLAME2,ENTITY_ORCSWORDSMANBLUE,ENTITY_SHIELDMAIDEN,ENTITY_CHAOSKNIGHT1,ENTITY_CHAOSKNIGHT2,ENTITY_OOZE,ENTITY_BOMBMAN1 = xrange(47)

    def __init__(self,x,y,w,h):
        self.x = x
        self.y = y
        self.w = w
        self.h = h

        self.images = []
        self.imagescounter = 0
        self.maximages = 0
        
        self.talksystem = None

	#self.platform = 0

	self.hitpoints = 100#FIXME in entities like trees
#NOTE        self.name = "Entity"

    def doplatform(self,player,level):
	1

    def dofood(self,player):
	1

    def leftbuttonpressed(self,game):#,mouse,text,press0):
        print "+++ left button press on entity"
        return None

    def leftbuttonpressedwithinventoryitem(self,game):
        print "+++ left button press on entity with inventory item"
        if game.mouse.press(0):
            return 1
        return None


    def middlebuttonpressed(self,mouse):
        1

    def rightbuttonpressed(self,mouse):
        1


    def getname(self,text):
        text.getname(self)

    def getnametext(self,text):
        return "Entity"

    def addimage(self,filename,r,g,b):# rgb transparency values
        self.maximages += 1

        image = pygame.image.load(filename).convert()
        image.set_colorkey((r,g,b))
        self.images.append(image)
        
#    def auxdraw(self):
#	self.auxdraw(self)

    def draw(self,screen):
        self.imagescounter += 1
        if self.imagescounter == self.maximages:
            self.imagescounter = 0     
        screen.scr.blit(self.images[self.imagescounter], (self.x, self.y))

    def drawonmap(self, screen, maproom):
        self.imagescounter += 1
        if self.imagescounter == self.maximages:
            self.imagescounter = 0     
        screen.scr.blit(self.images[self.imagescounter], (self.x+maproom.relativex, self.y+maproom.relativey))

    
    def update(self,room):#FIXME all level arg must be room

        r = randint(0,3)
        if r == 0:
            self.x += 1
        if r == 1:
            self.x -= 1
        if r == 2:
            self.y += 1
        if r == 3:
            self.y -= 1

    def decreasehpinplayer(self, player, level):
	1	
    
    def hit(self):
        1

    def bounceback(self):
        1

    def remove(self,list):
        1

    def deathupdate(self):
        1

    def destroy(self):
        1

    def collisionlist(self,collider,list):
        for i in range(0,list.length):
            o = None
            o = list.getlistobject(o)
            if o and collider.x >= o.x and collider.x <= o.x + o.w and collider.y >= o.y and collider.y <= o.y + o.h:   	
                return o
        return None

    def addtolist(self,list):
        list.addobject(self)

    def isskeleton(self):
        return None

    def isplayerskeleton(self):
        return None

    # FIXME enemy
    def usedracolichspellbook(self,game):
        game.screen.scr.blit(game.font.render("Nothing happens!", 16, (255,255,255)), (10,10))
	return "Nothing happens!"	
        
    def useyellowring(self,game):
        game.screen.scr.blit(game.font.render("Nothing happens!", 16, (255,255,255)), (10,10))
	return "Nothing happens!"	
    
    def usescrollinvisibility(self,game):
        game.screen.scr.blit(game.font.render("Nothing happens!", 16, (255,255,255)), (10,10))
	return "Nothing happens!"	

    def setblackplayer(self,game):
        print "+++ set black entity"
        1


	# FOR mouse CLICKS WITH ITEMS
    def leftbuttonpressedwithinventoryitem(self,game):
        if game.mouse.press(0) == 1:
            self.talkcounter += 1
            if game.inventoryitem:
                game.inventoryitem.use(self,game)
            return 1
        else:
            self.talkcounter = 0 	
            return None#self.currenttext



# Copyright (C) Johan Ceuppens 2011
# Copyright (C) Johan Ceuppens 2010

# Copyright (C) Johan Ceuppens 2009 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


from box import *

class Exitbox(Box):
    "exit box"
    def __init__(self,x,y,w,h,roomid,newx,newy,room):
        Box.__init__(self,x,y,w,h)
        
        self.roomid = roomid
        self.newx = newx
        self.newy = newy
        self.room = room

# Copyright (C) Johan Ceuppens 2011
# Copyright (C) Johan Ceuppens 2010

# Copyright (C) Johan Ceuppens 2009,2010 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import pygame
from pygame.locals import *

from entity import *

from random import *
import os

from utility import *

class Food(Entity):
    def __init__(self,x,y,w,h):
        Entity.__init__(self,x,y,w,h)
	if randint(0,1) == 0:
            self.addimage('./pics/food3.bmp',255,255,255)
        else:
            self.addimage('./pics/food3.bmp',255,255,255)
#        self.addimage('food3.bmp',0,0,0)

    def update(self,level):
        1

    def deathupdate(self):
        1
        
    def remove(self,list):
	print ("You eat some food.\n")
        list.remove(self)
	return None
    
    def dofood(self,player):
	player.increasehp()
 
    def getname(self,text):
        return text.getfoodname()

# Copyright (C) Johan Ceuppens 2011
# Copyright (C) Johan Ceuppens 2010

# Copyright (C) Johan Ceuppens 2009 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import pygame
from pygame.locals import *
from bombman import *
from player import *
from level import *
from screen import *
#from utility import *
from mouse import *
from textenglish import *
#from textdutch import *
from inventory import *
from inventoryscrollinvisibility import *

class Game:
    "Main function"
    def __init__(self):
        pygame.init()
        pygame.font.init()
        self.font = pygame.font.SysFont("Vera", 32)
	self.counter = 0
        self.language = "english"
        
#        if self.language == "english":
        self.text = Textenglish()
#        elif self.language == "dutch":
#            self.text = Textdutch()

        self.screen = Screen()
        self.mouse = Mouse()
	self.inventoryitemtext = None 
        self.inventoryitem = None

	self.swordplay = 0
	self.movedflag = 0

        self.player = Player()
        self.level = Level(self.screen,self.player,self.font)

       	blankimage = pygame.image.load('./pics/blank.bmp').convert()


	self.player.x = 320
	self.player.y = 250
##	while self.player.y > 250:
##            self.level.roommanager.room.collisionexits(self.player,self.level.roommanager)
##            self.level.roommanager.room.update(self.player,self.level, self.swordplay)                
##            self.level.roommanager.room.draw(self.screen)
##	    self.player.moveupanddirect1()
##	    self.screen.scr.blit(self.player.draw(), (self.player.x,self.player.y))
##            pygame.display.update()
##            self.screen.scr.blit(blankimage, (0,0))

        while 1:#self.level.gameover == 0:

            for event in pygame.event.get():
                if event.type == QUIT:
                    return
                elif event.type == KEYDOWN:
			for i in range(0,self.level.roommanager.room.trees.length):
                    		tempoct = self.level.roommanager.room.collisionboxes(self.player) or self.level.roommanager.room.trees.getwithindex(i).collision(self.player,self)
		    		self.movedflag = 1
                    		if event.key == K_SPACE:
		       			if tempoct == 1:## or tempoct == 1:
						self.player.jump(self,1)
		       			else:
						self.player.jump(self,0)
					break
                    		if event.key == K_UP:
		           		if tempoct == 1: 
                           			self.player.bounceback(self.level)
      	                        		print "Collision player with box"
			   		elif tempoct == 2:
                        			self.player.moveupanddirect(self.level)
						self.player.depth += 1
                     	   		else:
                        			self.player.moveupanddirect(self.level)
					break
                    		elif event.key == K_DOWN:
                    	   		tempoct2 = self.level.roommanager.room.collisionboxes(self.player) or self.level.roommanager.room.octtree.collision(self.player,self)
                       	   		if tempoct2:
                       	        		self.player.movedownanddirect(self.level)
						self.player.depth += 1
			   		elif not tempoct2:
						self.player.jump(self,0)
                           		elif tempoct == 1:
			   			self.player.bounceback(self.level)
               	                		print "Collision player with box"
			   		elif tempoct == 2:
                        			self.player.movedownanddirect(self.level)
					break
                    		elif event.key == K_LEFT:
                           		if tempoct == 1:
						self.player.bounceback(self.level)
                   	       			print "Collision player with box"
                           		else:
                        			self.player.moveleftanddirect(self.level)
					break
                    		elif event.key == K_RIGHT:
                           		if tempoct == 1:
						self.player.bounceback(self.level)
                                		print "Collision player with box"
                           		else:
                        			self.player.moverightanddirect(self.level)

					break
                    		elif event.key == K_u:
#                        self.level.gameover = 1
                      #FIXME  pygame.event.flush()
                       			flag = 0
                        		inventory = Inventory()
		
					if Scrollinvisibility(0,0,0,0,"1","1").readkeys(None):
                            			inventory.additem(Inventoryscrollinvisibility())
	
					while flag == 0:#NOTE1
                            			for event in pygame.event.get():
                               		 		if event.type == QUIT:
                               		     			return

                             		   		elif event.type == KEYDOWN:
                                    				if event.key == K_LEFT:
                                        				inventory.moveleft()
                                    				elif event.key == K_RIGHT:
                                        				inventory.moveright()
                                    				elif event.key == K_RETURN:
                                        				self.inventoryitem = inventory.getitem(self.inventoryitem)

                                        		self.level.gameover = 0
                                        		flag = 1


                                	inventory.draw(self.screen)
                                	pygame.display.update()
					break
 

	    ### FIX trees
          ###  temp1 = self.level.roommanager.room.octtree.collision(self.player,self)
###	    if temp1 == 2 or temp1 == 1:
		1
	    ###else:
	###	self.player.jump(self,0)###FIX self.trees
			
		1###if self.player.depth < 0:
            self.level.roommanager.room.collisionexits(self.player,self.level.roommanager)
            self.level.roommanager.room.update(self.player,self.level, self.swordplay)                
            self.level.roommanager.room.draw(self.screen)
            ### fall         
            ###if not self.level.roommanager.room.octtree.collisiony(self.player):
	###	self.player.y -= 1	

###	    bombman = Bombman(400,100)
###            if bombman.draw(self.level.roommanager.room):
###		1

            if self.movedflag:
		self.movedflag = 0 
		self.screen.scr.blit(self.player.draw(), (self.player.x,self.player.y))
            else:
		self.screen.scr.blit(self.player.draw0(), (self.player.x,self.player.y))

            self.mouse.update()
            self.mouse.draw(self.screen,self.inventoryitem)
            
            # FIXME1 maproom 
            if self.inventoryitem:
                self.inventoryitemtext = self.level.roommanager.room.domouseinventory(self)
		if self.inventoryitemtext:
		    self.counter = 100
                    self.inventoryitem = None
            else:
                self.level.roommanager.room.domouse(self)#self.screen, self.mouse,font1,self.text,self.player)

#self.level.roommanager.room.domouse(self)#self.screen, self.mouse,font1,self.text,self.player)
	    if self.inventoryitemtext and self.counter > 0:
		self.counter -= 1
		self.screen.scr.blit(self.font.render(self.inventoryitemtext, 16, (255,255,255)),(10,10))
            pygame.display.update()
            self.screen.scr.blit(blankimage, (0,0))
       
#	while 1: 
#       	   image1 = pygame.image.load('gameover1.bmp').convert()
#           self.screen.scr.blit(image1, (0,0))
#           pygame.display.update()

        
        
        if not Utilities().readandfindstring(string):
                    
            f = open("./workfile",'r')
            for line in f:
                s += line
                
                f = open("./workfile",'w')
        
                f.write(s)
                f.write(string)
                f.write("\n")
                

    def domouseonplayer(self,room):
        room.collisionplayer(self.mouse,self.player)

    def domouseonplayerrelative(self,room):
        room.collisionplayerrelative(self.mouse,self.player,room.relativex,room.relativey)


    def leftbuttonpressed(self,o):
        self.previoustext = o.leftbuttonpressed(self)#self,game.text,self.press(0))
        return self.previoustext
                        
    def collisionlistonmouse(self,list):
        for i in range(0,list.length):
            o = None
            #o = list.getlistobject(o)
	    o = list.getwithindex(i)

            if o and self.mouse.x >= o.x and self.mouse.x <= o.x + o.w and self.mouse.y >= o.y and self.mouse.y <= o.y + o.h:   	
                return o
        return None

    # Relative processors
    def processorrelativex(self,o,relx):
        return o.x - relx

    def processorrelativey(self,o,rely):
        return o.y - rely

    def collisionlistrelativeonmouse(self,list,relx,rely):
        for i in range(0,list.length):
            o = None
            o = list.getlistobject(o)

            if o:
            
                x = o.x
                y = o.y
                mx = self.processorrelativex(self.mouse,relx)
                my = self.processorrelativey(self.mouse,rely)

                if o and mx >= x and mx <= x + o.w and my >= y and my <= y + o.h:   	
                    return o
        return None



    def update(self):
        "move the fist based on the mouse position"
        
#        pos = pygame.mouse.get_pos()
#        self.rect.midtop = pos
#        if self.punching:
#            self.rect.move_ip(5, 10)
    

if __name__ == "__main__":
   foo = Game()

# Copyright (C) Johan Ceuppens 2011
# Copyright (C) Johan Ceuppens 2010

# Copyright (C) Johan Ceuppens 2009 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from graph import *

class Graphbombman(object):
    def __init__(self):
        self.graph = None
        self.currentnode = None

    def get(self,text):

        if not self.graph or not self.currentnode:#runtime load of textgraph
            self.graph = Graph(text.getbombman1())
            self.currentnode = self.graph
            self.graph.next.next = Graphnode(text.getbombman2())

        if self.currentnode.next:# dummy start of queue in graph.py
            self.currentnode = self.currentnode.next
            return self.currentnode.node
        else:
            self.currentnode = self.graph
        return None
            

# Copyright (C) Johan Ceuppens 2011
# Copyright (C) Johan Ceuppens 2010

# Copyright (C) Johan Ceuppens 2009 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from graph import *

class Graphcapricornguardian3(object):
    def __init__(self):
        self.graph = None
        self.currentnode = None

    def get(self,text):

        if not self.graph or not self.currentnode:#runtime load of textgraph
            self.graph = Graph(text.getcapricornguardian31())
            self.currentnode = self.graph
            self.graph.next.next = Graphnode(text.getcapricornguardian32())

        if self.currentnode.next:# dummy start of queue in graph.py
            self.currentnode = self.currentnode.next
            return self.currentnode.node
        else:
            self.currentnode = self.graph
        return None
            

# Copyright (C) Johan Ceuppens 2011
# Copyright (C) Johan Ceuppens 2010

# Copyright (C) Johan Ceuppens 2009 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from graph import *

class Graphmyconid(object):
    def __init__(self):
        self.graph = None
        self.currentnode = None

    def get(self,text):

        if not self.graph or not self.currentnode:#runtime load of textgraph
            self.graph = Graph(text.getmyconid1())
            self.currentnode = self.graph
            self.graph.next.next = Graphnode(text.getmyconid2())

        if self.currentnode.next:# dummy start of queue in graph.py
            self.currentnode = self.currentnode.next
            return self.currentnode.node
        else:
            self.currentnode = self.graph
        return None
            

# Copyright (C) Johan Ceuppens 2011
# Copyright (C) Johan Ceuppens 2010

# Copyright (C) Johan Ceuppens 2009 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


class Graphnode(object):
    def __init__(self, o):#pass
        self.node = o
        self.next = None

class Graph(Graphnode):
    def __init__(self,o):
        Graphnode.__init__(self,"dummy")
        self.next = Graphnode(o)

# Copyright (C) Johan Ceuppens 2011
# Copyright (C) Johan Ceuppens 2010

# Copyright (C) Johan Ceuppens 2009 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import pygame
from pygame.locals import *

from inventoryitem import *

class Inventorybomb(Inventoryitem):
    def __init__(self):
        Inventoryitem.__init__(self, "./pics/bomb1.bmp")

    def use(self,e,game):
        print 'You used a bomb on %s' % e

# Copyright (C) Johan Ceuppens 2011
# Copyright (C) Johan Ceuppens 2010

# Copyright (C) Johan Ceuppens 2009 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import pygame
from pygame.locals import *


class Inventoryitem(object):
    def __init__(self,imagefilename):
        self.image = pygame.image.load(imagefilename).convert()
        self.image.set_colorkey((0,0,0))#FIXME rgb transdparency


    def draw(self,screen,x,y):
        screen.scr.blit(self.image, (x, y))

    def addtolist(self,list):
        list.addobject(self)


#NOTE There s no fallthrough for item usage

# Copyright (C) Johan Ceuppens 2011
# Copyright (C) Johan Ceuppens 2010

# Copyright (C) Johan Ceuppens 2009 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import pygame
from pygame.locals import *
from list import *
from inventorybomb import *

class Inventory(object):
    def __init__(self):
        self.background = pygame.image.load('./pics/room1.1.bmp').convert()
        self.list = List()
        self.list.addobject(Inventorybomb())

        self.rectimage = pygame.image.load('./pics/rect1.bmp').convert()
        self.rectimage.set_colorkey((0,0,0))
        self.selectioncounter = 1

    def draw(self,screen):

        screen.scr.blit(self.background, (0, 0))

        self.drawrect(screen,self.selectioncounter*80,120)

        for i in range(0,self.list.length):
            o = self.list.getwithindex(i)
# pass by value FIXME
#             o = None
#             o = self.list.getlistobject(o)
            if o:
                o.draw(screen,80*(i+1),120)



    def drawrect(self,screen,x,y):
        screen.scr.blit(self.rectimage, (x, y))


    def moveleft(self):
        if self.selectioncounter > 1:
            self.selectioncounter -= 1

    def moveright(self):
        if self.selectioncounter < 8:
            self.selectioncounter += 1

#    def setselection(self):
#        i = 
        
    def getitem(self,o):
#        for i in range(0,self.selectioncounter):
#            o = None
#            o = self.list.getlistobject(o)
        o = self.list.getwithindex(self.selectioncounter-1) 
        if o:
            return o

        return None

    def additem(self,o):
        self.list.addobject(o)

# Copyright (C) Johan Ceuppens 2011
# Copyright (C) Johan Ceuppens 2010

# Copyright (C) Johan Ceuppens 2009 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import pygame
from pygame.locals import *

from inventoryitem import *

class Inventoryscrollinvisibility(Inventoryitem):
    def __init__(self):
        Inventoryitem.__init__(self, "./pics/scroll1.bmp")

    def use(self,e,game):
        print 'You used a scroll of invisibility on %s' % e
        if e:
            return e.usescrollinvisibility(game)
	

# Copyright (C) Johan Ceuppens 2011
# Copyright (C) Johan Ceuppens 2010

# Copyright (C) Johan Ceuppens 2009 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import pygame
from pygame.locals import *
from roommanager import *

class Level:
    "Level"
    def __init__(self, screen, player,font):
        self.screen = screen
        self.roommanager = Roommanager(screen,player,font)

        self.gameover = 0

    def update(self,entity):
       # img = pygame.image.load('rect1.bmp').convert()
       # rect.set_colorkey((0,0,0))
       # rect  = pygame.draw.rect(self.screen, 0x0ff0000)
       # bg = pygame.image.load('bg0.bmp').convert()
       # self.screen.scr.blit(bg, (0, 0))
       # screen.draw(self.entities)
       # pygame.display.update()
	1

    def draw(self,entity):
        1
        
    def setgameover(self):
	print ("game over")
        self.gameover = 1
        1

# Copyright (C) Johan Ceuppens 2011
# Copyright (C) Johan Ceuppens 2010

# Copyright (C) Johan Ceuppens 2009 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


class List:
    def __init__(self):
        self.list = []
        self.length = 0
        self.index = 0

    def add(self,o):
        o.addtolist(self)

    def addobject(self,o):
        self.length += 1
        self.list.append(o)
    
    def remove(self,o):
        resultlist = []
        self.length = 0
        
        for el in self.list:
            if  el != o:
                resultlist.append(el)
                self.length += 1
        self.list = resultlist
                

    def getlistobject(self,o):#NOTE1 o not list
        if self.index < self.length:
            o = self.list[self.index]
            self.index += 1
            return o
        else:
            self.index = 0
        return None

    def getwithindex(self,index):
        if index < self.length:
            return self.list[index]

        return None

#    def resetindex(self,idx):
#        self.index = idx
        

# Copyright (C) Johan Ceuppens 2011
# Copyright (C) Johan Ceuppens 2010

# Copyright (C) Johan Ceuppens 2009 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import pygame
from pygame.locals import *
from room import *
from octtree import *

class Maproom(Room):
    "Room with a (big) map"
    def __init__(self,x,y,font):
        Room.__init__(self)
        self.relativex = x
        self.relativey = y
	self.offset = 0#200 KLUDGE
	### self.roomman = roomman
	###FIXself.octtree = Octtree(1000,1000,1000,1000,1000,1000,3,self,font)
	###self.octtree = Octtree(1000,1000,1000,1000,1000,1000,0,self)
	self.trees = List()

    def draw(self,screen):
            ##if self.level.roommanager.room.octtree.walkin(self.player):
	    ###	print "Collision!"
	###	self.level.roommanager.room.roomman.room.undomove(self.player)
        rotx = 2/sqrt(2)###-3.141528/2 
        roty = 0#2/sqrt(2)###-3.141528/2 
        rotz = 0#2/sqrt(2)###-3.141528/2 
	    ##roty = 0 
	    ##rotz = 0 
	screen.scr.blit(self.background, (self.offset+self.relativex, 0+self.relativey))
	for o in self.trees.list:
        	if o.draw(self.player,screen,rotx,roty,rotz):
			1

        screen.drawonmap(self)

    def update(self,player,level,swordplay):
        self.updatelist(self.entities)

        o = player.collisionlistrelative(self.entities,self.relativex,self.relativey)
###        if o:
###	    if swordplay != 0:
###		print("+++++++++++++++SWORDPLAY")
###		o.hit()#points -= 1
  ###      	if o.hitpoints <= 0:
    ###        	    self.entities.remove(o)    
###	    else:
###	        o.dofood(player)
###	    	o.doplatform(player,level)
###               	player.bounceback(level)
##                o.decreasehpinplayer(player,level)
#            	o.remove(self.entities)

	## FIXME memento pattern
    def undomove(self,player):
	if player.direction == "north":   
        	self.relativey = self.relativey - 10
        	self.relativey = self.relativey - 10
        	self.relativey = self.relativey - 10
	if player.direction == "south":     
        	self.relativey = self.relativey + 10
        	self.relativey = self.relativey + 10
        	self.relativey = self.relativey + 10
	if player.direction == "west":     
        	self.relativex = self.relativex - 10
        	self.relativex = self.relativex - 10
        	self.relativex = self.relativex - 10
	if player.direction == "east":     
        	self.relativex = self.relativex + 10
        	self.relativex = self.relativex + 10
        	self.relativex = self.relativex + 10
 
    def moveup(self,player):
        player.direction = "north"
###	if self.relativey > 0:###FIXME
       	self.relativey = self.relativey + 10

    def movedown(self,player):
        player.direction = "south"
        self.relativey = self.relativey - 10

    def moveleft(self,player):
        player.direction = "west"
        self.relativex = self.relativex + 10

    def moveright(self,player):
        player.direction = "east"
        self.relativex = self.relativex - 10


    #### COLLISION CODE - OVERRIDING ROOM CODE 


    def processorrelativex(self,o):
        return o.x - self.relativex

    def processorrelativey(self,o):
        return o.y - self.relativey

    def collisionlistbombrelative(self,bomb,list):
        for o in list.list:
            if o:
                x = self.processorrelativex(o)
                y = self.processorrelativey(o)
                if x + 12>= bomb.x - bomb.w and x <= bomb.x + bomb.w +12 and y+12 >= bomb.y - bomb.h and y <= bomb.y + bomb.h +12:
                    return o
        return None

#     def warp(self,x,y):
#         self.relativex = 800
#         self.relativey = 800


    def collisionexits(self,player,roommanager):
        o = player.collisionlistrelative(self.exits, self.relativex, self.relativey)

        if o:
            print 'exit foo x=%d,y=%d px=%d py=%d' % (o.x,o.y,player.x,player.y)
            o.room.loadroom()
            roommanager.room = o.room
            player.warp(o.newx,o.newy)
            return 1
        return None

    def collisionboxes(self,player):
        o = player.collisionlistrelative(self.boxes, self.relativex, self.relativey)
        if o:
            #player.bounceback()
            return 1
        return None


    def collisionandremoveenemieswithbomb(self, bomb, player, damagevalue):

        b = Bomb(self.processorrelativex(bomb),self.processorrelativey(bomb),80,80,player)
#        bomb.x = self.processorrelativex(bomb.x)
#        bomb.y = self.processorrelativey(bomb.y)
        o = self.collisionlistbombrelative(b,self.entities)
        if o:
            retvalue = o.hit()
	    o.hitcommand(player, "bomb",retvalue, damagevalue)
            o.remove(self.entities)
            ### self.entities.remove(o)
            
    def summonskeletonunderplayercommand(self,x,y):
        self.addenemy(Skeletonunderplayercommand(x,y,50,50))


    def summonskeleton(self,x,y):
        self.addenemy(Skeleton2(x-self.relativex,y-self.relativey,50,50))


    # Dropping bomb entities
    def addsprite(self,sprite):
        sprite.x -= self.relativex
        sprite.y -= self.relativey
        self.addentity(sprite)


    def collisionplayerrelative(self,mouse,player,relx,rely):
        return mouse.collisionplayerrelative(player,relx,rely)

    def domouse(self,game):
        # Collide mouse with player
        p = game.domouseonplayerrelative(self)#game.collisionplayerrelative(self)
        if p:
            print ("mouse click on player 1")
            foo = game.leftbuttonpressed(game.player)
            if foo:
		print ("mouse click on player 12")
                return
            
        # Collide mouse with entities
        o = None
        o = game.collisionlistrelativeonmouse(self.entities,self.relativex,self.relativey)
        if o:
            foo = game.mouse.leftbuttonpressed(o,game)
#            print foo
            if foo:
		### BLIT RETURN TALK TEXT
                game.screen.scr.blit(game.font.render(foo, 16, (255,255,255)), (20,330))
#                print foo
            else:
                game.screen.scr.blit(game.font.render(o.getname(game.text), 10, (0,0,0)), (game.mouse.x+10,game.mouse.y))
#            mouse.middlebuttonpressed(o,text)
#            mouse.rightbuttonpressed(o,text)
            
                
    def domouseinventory(self,game):#,screen,mouse,font,text,item,player):
        # Collide mouse with player
        p = game.domouseonplayerrelative(self)
        if p:
            footext4 = game.mouse.leftbuttonpressedwithinventoryitem(game.player,game)
            if footext4 and footext4 != "":
                return footext4 

        # Collide mouse with entities
        o = None
        o = game.collisionlistrelativeonmouse(self.entities,self.relativex,self.relativey)
        if o and game.inventoryitem:
            footext5 = game.mouse.leftbuttonpressedwithinventoryitem(o,game)#.text,game.inventoryitem)
            if footext5 and footext5 != "":
                return footext5 

        if not o and game.mouse.press(0):
            return 1

        return None

import pygame, sys,os
from pygame.locals import *

from vector3 import *

class Matrix3():
    def __init__(self, xx1,yy1,zz1,xx2,yy2,zz2,xx3,yy3,zz3):
	self.array = [xx1,yy1,zz1,xx2,yy2,zz2,xx3,yy3,zz3]


    def transpose(self):
	m = Matrix3(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)
	array = [0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0]
	array[0] = self.array[0]
	array[1] = self.array[3]
	array[2] = self.array[6]
	array[3] = self.array[1]
	array[4] = self.array[4]
	array[5] = self.array[7]
	array[6] = self.array[2]
	array[7] = self.array[5]
	array[8] = self.array[8]
	m.array = array
        return m	

    def multiply(self, v1):
	retv = Vector3(0.0,0.0,0.0)
	for j in [0,1,2]:
	    for i in [0,1,2]:
	        retv.array[i] += self.array[j*3+i] * v1.array[i]
	return retv

    def multiplyByMatrix(self, m1):#FIXME sum
	retm = Matrix3(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)
	m = m1.transpose()
	for k in [0,1,2,3,4,5,6,7,8]:
	    for j in [0,1,2]:
	        retm.array[k] += self.array[j] * m.array[j]
	return retm
    

# Copyright (C) Johan Ceuppens 2011
# Copyright (C) Johan Ceuppens 2010

# Copyright (C) Johan Ceuppens 2009 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import pygame
from pygame.locals import *


class Mouse(object):
    def __init__(self):
        self.position = None
        self.x = None
        self.y = None
        # for collision code
        self.w = 20
        self.h = 20
        self.update()
#        self.pressed = None
        self.previoustext = None

    def update(self):
        self.position = pygame.mouse.get_pos()
        self.x = self.position[0]
        self.y = self.position[1]

    def draw(self,screen,item):
        if item:
            screen.scr.blit(item.image, (self.x, self.y))

    # FIXME copy-paste entity
#     def collisionlist(self,collider,list):
#         for i in range(0,list.length):
#             o = None
#             o = list.getlistobject(o)
#             if o and collider.x >= o.x and collider.x <= o.x + o.w and collider.y >= o.y and collider.y <= o.y + o.h:   	
#                 return o
#         return None

#     def collisionlistrelative(self,collider,list,relx,rely):
#         for i in range(0,list.length):
#             o = None
#             o = list.getlistobject(o)

#             if o:
#                 #FIXME2
#                 x = o.x#self.processorrelativex(o,relx)
#                 y = o.y#self.processorrelativey(o,rely)

#                 mx = self.processorrelativex(self,relx)
#                 my = self.processorrelativey(self,rely)

#                 if mx >= x and mx <= x + o.w and my >= y and my <= y + o.h:
#                     return o
#         return None



    def collisionplayer(self,player):
        return player.collisionplayer(self)

    def collisionplayerrelative(self,player,relx,rely):
        return player.collisionplayerrelative(self,relx,rely)

    # generic method
    def press(self,buttonnumber):
        if  pygame.mouse.get_pressed()[buttonnumber]:
            return 1
            
        return None

    # left button press on player or entity
    def leftbuttonpressed(self,o,game):
        self.previoustext = o.leftbuttonpressed(game)#self,game.text,self.press(0))
        return self.previoustext
        
    def leftbuttonpressedwithinventoryitem(self,o,game):
        self.previoustext = o.leftbuttonpressedwithinventoryitem(game)
        return self.previoustext

#  

#     def middlebuttonpressed(self,o):
#         o.middlebutonpressed(self)

#     def rightbuttonpressed(self,o):
#         o.rightbutonpressed(self)

# Copyright (C) Johan Ceuppens 2011
# Copyright (C) Johan Ceuppens 2010

# Copyright (C) Johan Ceuppens 2009 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


from enemytalking import *
from talksystemmyconid import *
from entityfactory import *

class Myconid(Enemytalking):
    def __init__(self,x,y,w,h):
        Enemytalking.__init__(self,x,y,w,h,Talksystemmyconid())
        self.addimage('./pics/myconid1.bmp',0,0,0)
        self.addimage('./pics/myconid2.bmp',0,0,0)
        self.addimage('./pics/myconid3.bmp',0,0,0)
        self.counter = 0


####### TEXT / FONT interface
    def getname(self,text):
        return text.getmyconidname()

#    def gettalk(self):
#        talksystem.gettalk(self)


####### other methods
    def draw(self,screen):
        self.counter += 1

        if self.counter >= 40:
            self.counter = 0

        if self.counter < 10:
            screen.scr.blit(self.images[0], (self.x, self.y))
        elif self.counter < 20:
            screen.scr.blit(self.images[1], (self.x, self.y))
        elif self.counter < 30:
            screen.scr.blit(self.images[2], (self.x, self.y))
        elif self.counter < 40:
            screen.scr.blit(self.images[1], (self.x, self.y))
        

#     def leftbuttonpressed(self,mouse,text):
#         return self.talksystem.gettalk(text)#foo

    def remove(self,list):
        if self.hitpoints <= 0:
            list.remove(self)

	    # LEAVE & ADD FOOD AT RANDOM
	    if randint(0,4) == 3:
		print ("The myconid drops some food.")
        	#factory = Entityfactory()
                #o = factory.make(Entity.ENTITY_FOOD,
		o = Food(self.x,self.y,20,20)#FIXME Maproom x, y!
	        list.add(o)
	
    def leftbuttonpressedwithinventoryitem(self,game):
        text = Enemytalking.leftbuttonpressedwithinventoryitem(self,game)
	if text:
            print "You use the item on the myconid."
            return text
        else:
            return None

# Copyright (C) Johan Ceuppens 2011
# Copyright (C) Johan Ceuppens 2010

# Copyright (C) Johan Ceuppens 2009 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from list import *

class OcttreeLeaf:
    "Octotree Leaf"
    def __init__(self, x,y,z,w,h,d):
	self.x = x 
	self.y = y 
	self.z = z 
	self.w = w 
	self.h = h 
	self.d = d 
	self.collide = 0 

    def isleaf(self):
	return 1

class OcttreeNode(OcttreeLeaf):
    "Octotree Node"
    def __init__(self,x,y,z,w,h,d):
        OcttreeLeaf.__init__(self,x,y,z,w,h,d)
	self.nodes = List() 

    def isleaf(self):
	return 0

    def add(self,n):
        self.nodes.addobject(n)


class Octtree:
    "Octtree"
    def __init__(self,x,y,z,w,h,d,depth):
	self.tree = OcttreeNode(x,y,z,w,h,d)
	self.depth = depth
	
    def add(self,n):
        self.tree.addobject(n)

    def generatetree(self,depth,width):
	self.generatetreerec(depth,self.tree,width,self.x,self.y,self.z,self.w,self.h,self.d)

    def generatetreeroom1(self,depth,width):
	self.generatetreerecroom1(depth,self.tree,self.tree.x,self.tree.y,self.tree.z,self.tree.w,self.tree.h,self.tree.d,300)

    def generatetreerec(self,depth,node,x,y,z,w,h,d):
	if depth <= 0:
		node.add(OcttreeLeaf(x,y,z,w,h,d))
		return 
###	for i in range(0,9):
	node.add(OcttreeNode(x,y,z,w/3,h/3,d/3))
	node.add(OcttreeNode(4*x/3,y,z,w/3,h/3,d/3))
	node.add(OcttreeNode(5*x/3,y,z,w/3,h/3,d/3))
	node.add(OcttreeNode(x,4*y/3,z,w/3,h/3,d/3))
	node.add(OcttreeNode(x,5*y/3,z,w/3,h/3,d/3))
	node.add(OcttreeNode(x,y,z,w/3,h/3,d/3))
	node.add(OcttreeNode(x,y,4*z/3,w/3,h/3,d/3))
	node.add(OcttreeNode(x,y,5*z/3,w/3,h/3,d/3))
##		print "-> %d" % i
	self.generatetreerec(depth-1,node.nodes.getwithindex(0),x,y,z,w/3,h/3,d/3)
	self.generatetreerec(depth-1,node.nodes.getwithindex(1),4*x/3,y,z,w/3,h/3,d/3)
	self.generatetreerec(depth-1,node.nodes.getwithindex(2),5*x/3,y,z,w/3,h/3,d/3)
	self.generatetreerec(depth-1,node.nodes.getwithindex(3),x,4*y/3,z,w/3,h/3,d/3)
	self.generatetreerec(depth-1,node.nodes.getwithindex(4),x,5*y/3,z,w/3,h/3,d/3)
	self.generatetreerec(depth-1,node.nodes.getwithindex(5),x,y,z,w/3,h/3,d/3)
	self.generatetreerec(depth-1,node.nodes.getwithindex(6),x,y,4*z/3,w/3,h/3,d/3)
	self.generatetreerec(depth-1,node.nodes.getwithindex(7),x,y,5*z/3,w/3,h/3,d/3)


    def generatetreerecroom1(self,depth,node,x,y,z,w,h,d,border):
	if depth <= 0:
		node.add(OcttreeLeaf(x,y,z,w,h,d))
		return 
	node.add(OcttreeNode(x,y,z,w/3,h/3,d/3))
	node.add(OcttreeNode(4*x/3,y,z,w/3,h/3,d/3))
	node.add(OcttreeNode(5*x/3,y,z,w/3,h/3,d/3))
	node.add(OcttreeNode(x,4*y/3,z,w/3,h/3,d/3))
	node.add(OcttreeNode(x,5*y/3,z,w/3,h/3,d/3))
	node.add(OcttreeNode(x,y,z,w/3,h/3,d/3))
	node.add(OcttreeNode(x,y,4*z/3,w/3,h/3,d/3))
	node.add(OcttreeNode(x,y,5*z/3,w/3,h/3,d/3))
	if w < border:
		node.collide = 1
	##print "-> %d" % i
	self.generatetreerec(depth-1,node.nodes.getwithindex(0),x,y,z,w/3,h/3,d/3)
	self.generatetreerec(depth-1,node.nodes.getwithindex(1),4*x/3,y,z,w/3,h/3,d/3)
	self.generatetreerec(depth-1,node.nodes.getwithindex(2),5*x/3,y,z,w/3,h/3,d/3)
	self.generatetreerec(depth-1,node.nodes.getwithindex(3),x,4*y/3,z,w/3,h/3,d/3)
	self.generatetreerec(depth-1,node.nodes.getwithindex(4),x,5*y/3,z,w/3,h/3,d/3)
	self.generatetreerec(depth-1,node.nodes.getwithindex(5),x,y,z,w/3,h/3,d/3)
	self.generatetreerec(depth-1,node.nodes.getwithindex(6),x,y,4*z/3,w/3,h/3,d/3)
	self.generatetreerec(depth-1,node.nodes.getwithindex(7),x,y,5*z/3,w/3,h/3,d/3)


    def collision(self,x,y,z):
	self.collisionrec(self.depth,self.tree,x,y,z)

    def collisionrec(self,depth,node,x,y,z):
	if depth <= 1:
		if node.collide == 1:
			return 1
		else:
			return 0
	for i in range(0,8):
		print "-> %d" % i
		if x < node.x and y < node.y and z < node.z:
			if node.collide == 1:
				return 1
		elif node.isleaf():
			return 0 
		else:
			self.collisionrec(depth-1,node.nodes.getwithindex(i),x,y,z)	

# Copyright (C) Johan Ceuppens 2011
# Copyright (C) Johan Ceuppens 2010

# Copyright (C) Johan Ceuppens 2009 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from list import *
from engine import *
from pygame import *
from pygame.locals import *
import pygame

class OcttreeLeaf:
    "Octotree Leaf"
    def __init__(self, x,y,z,w,h,d,depth):
	self.x = x 
	self.y = y 
	self.z = z 
	self.w = w 
	self.h = h 
	self.d = d 
	self.depth = 0
	self.collide = 0 
	
    def isleaf(self):
	return 1

class OcttreeNode(OcttreeLeaf):
    "Octotree Node"
    def __init__(self,x,y,z,w,h,d,depth):
        OcttreeLeaf.__init__(self,x,y,z,w,h,d,depth)
	self.nodes = List() 
	self.collide = 0 

    def isleaf(self):
	return 0

    def add(self,n):
        self.nodes.addobject(n)


class Octtree:
    "Octtree"
    def __init__(self,x,y,z,w,h,d,depth,room):
	self.tree = OcttreeNode(x,y,z,w,h,d,depth)
	self.depth = depth
	self.room = room
	self.diagonaloffset = 120
        self.image = pygame.image.load("./pics/tile1-rot-420x420.bmp").convert()
        self.image.set_colorkey((0,0,0))
	###FIXself.font  = room.roomman.font 
	
    def add(self,n):
        self.tree.addobject(n)

    def generatetree(self,border):
	self.generatetreerec(self.depth,self.tree,self.tree.x,self.tree.y,self.tree.z,self.tree.w,self.tree.h,self.tree.d,border)

    def generatetreerec(self,depth,node,x,y,z,w,h,d,border):
	if depth <= 0:###FIXME1:
		for i in range(1,4):
			node.add(OcttreeLeaf(i*x/4,   y/4,    z/4,    w/3,h/3,d/3,depth))
		for i in range(1,4):
			node.add(OcttreeLeaf(i*x/4,   i*y/4,    z/4,    w/3,h/3,d/3,depth))
		for i in range(1,4):
			node.add(OcttreeLeaf(i*x/4,   i*y/4,    i*z/4,    w/3,h/3,d/3,depth))
		for i in range(1,4):
			node.add(OcttreeLeaf(x/4,   i*y/4,    z/4,    w/3,h/3,d/3,depth))
		for i in range(1,4):
			node.add(OcttreeLeaf(x/4,   i*y/4,    i*z/4,    w/3,h/3,d/3,depth))
		for i in range(1,4):
			node.add(OcttreeLeaf(i*x/4,   y/4,    i*z/4,    w/3,h/3,d/3,depth))
		for i in range(1,4):
			node.add(OcttreeLeaf(x/4,   y/4,    i*z/4,    w/3,h/3,d/3,depth))
		### center node
		for i in range(0,21):
			tempnode = node.nodes.getwithindex(i)	
		#	###FIXif border < tempnode.x*(self.depth-1) + tempnode.w:### and border > tempnode.x: ### + w/3:
		#	print "leaf border %d\n" % (tempnode.x*(depth-1)
			### NOTE: vary for each room, e.g. depth search algorithm
			###FIXif border < tempnode.x + tempnode.w:
			###	border += border
			###	node.nodes.getwithindex(i).collide = 1
			if border < tempnode.x + tempnode.w:
				border += border
				node.nodes.getwithindex(i).collide = 1
		###node = leaf 
		return 
	for i in range(1,4):
		node.add(OcttreeNode(i*x/4,   y/4,    z/4,    w/3,h/3,d/3,depth))
	for i in range(1,4):
		node.add(OcttreeNode(i*x/4,   i*y/4,    z/4,    w/3,h/3,d/3,depth))
	for i in range(1,4):
		node.add(OcttreeNode(i*x/4,   i*y/4,    i*z/4,    w/3,h/3,d/3,depth))
	for i in range(1,4):
		node.add(OcttreeNode(x/4,   i*y/4,    z/4,    w/3,h/3,d/3,depth))
	for i in range(1,4):
		node.add(OcttreeNode(x/4,   i*y/4,    i*z/4,    w/3,h/3,d/3,depth))
	for i in range(1,4):
		node.add(OcttreeNode(i*x/4,   y/4,    i*z/4,    w/3,h/3,d/3,depth))
	for i in range(1,4):
		node.add(OcttreeNode(x/4,   y/4,    i*z/4,    w/3,h/3,d/3,depth))
	### center node
	###OBSOLETEfor i in range(0,21):
	###	tempnode = node.nodes.getwithindex(i)	
	###	if border < tempnode.x:
	###		node.nodes.getwithindex(i).collide = 1

	for i in range(1,4):
		self.generatetreerec(depth-1,node.nodes.getwithindex(i),i*x/4,    y/4,    z/4,   w/3,h/3,d/3,border)
	for i in range(1,4):
		self.generatetreerec(depth-1,node.nodes.getwithindex(i+2),i*x/4,    i*y/4,    z/4,   w/3,h/3,d/3,border)
	for i in range(1,4):
		self.generatetreerec(depth-1,node.nodes.getwithindex(i+5),i*x/4,    i*y/4,    i*z/4,   w/3,h/3,d/3,border)
	for i in range(1,4):
		self.generatetreerec(depth-1,node.nodes.getwithindex(i+8),x/4,    i*y/4,    z/4,   w/3,h/3,d/3,border)
	for i in range(1,4):
		self.generatetreerec(depth-1,node.nodes.getwithindex(i+11),x/4,    i*y/4,    i*z/4,   w/3,h/3,d/3,border)
	for i in range(1,4):
		self.generatetreerec(depth-1,node.nodes.getwithindex(i+14),i*x/4,    y/4,    i*z/4,   w/3,h/3,d/3,border)
	for i in range(1,4):
		self.generatetreerec(depth-1,node.nodes.getwithindex(i+17),x/4,    y/4,    i*z/4,   w/3,h/3,d/3,border)
	### center node


    def collision(self,player,game):
	self.result = 0
	self.collisionrec(self.depth,self.tree,player,game)
	return self.result

    def collisionx(self,player):
	self.result = 0
	self.collisionrecx(self.depth,self.tree,player.x,player.y,player.z)
	return self.result

    def collisiony(self,player):
	self.result = 0
	self.collisionrecy(self.depth,self.tree,player.x,player.y,player.z)
	return self.result

    def collisionrec(self,depth,node,player,game):
	x = player.x
	y = player.y
	z = player.z
	###if depth <= 1 or node.isleaf():
	relx = self.room.roomman.room.relativex
	rely = self.room.roomman.room.relativey
	###if node.isleaf():
	###print "leaf! depth=%d" % depth
	if (x + relx < node.x +  node.w*(-depth+1) and x + relx + node.w > node.x) and (y + rely < node.y + node.h*(-depth+1) and y + rely > node.y):
		print "collision player.depth=%d map.depth=%d!" % (player.depth,depth)
		if depth <= player.depth: 
		###if node.collide == 1:### and node.w > 333:
			self.result = 1
			return
		elif player.depth < depth:
		###			node.depth -= 1	
			self.result = 2
			return
		elif player.depth > depth:	
			self.result = 3
			return
		###	###self.result = 1###
		###	player.jump(game,0)
		
		###return #NOTE do not return for each node to be switched
	###elif node.isleaf():
	###	return
        else:		
		for i in range(0,21):
			if not node.isleaf():
	 			n = node.nodes.getwithindex(i)
				if n != None: 
					self.collisionrec(depth-1,n,player,game)
		return
	
    def collisionrecx(self,depth,node,x,y,z):
	###if depth <= 1 or node.isleaf():
	relx = self.room.roomman.room.relativex
	rely = self.room.roomman.room.relativey
	if node.isleaf():
		##print "leaf!"
		return
	for i in range(0,21):
		n = node.nodes.getwithindex(i)
		if (n != None):
			if (x < n.x + n.w and x > n.x): 
				print "collision!"
				if n.collide == 1:
					self.result = 1
					return
			self.collisionrecx(depth-1,n,x,y,z)	

    def collisionrecy(self,depth,node,x,y,z):
	###if depth <= 1 or node.isleaf():
	relx = self.room.roomman.room.relativex
	rely = self.room.roomman.room.relativey
	if node.isleaf():
		##print "leaf!"
		return
	for i in range(0,21):
		n = node.nodes.getwithindex(i)
		if (n != None):
			if (y < n.y + n.h and y > n.y): 
				print "collision!"
				if n.collide == 1:
					self.result = 1
					return
			self.collisionrecx(depth-1,n,x,y,z)	

    def walkin(self,player):
	self.result = 0
	self.walkinrec(self.depth,self.tree,player)
	return self.result

    def walkinrec(self,depth,node,player):
	px = player.x
	py = player.y
	pz = player.z
	relx = self.room.roomman.room.relativex
	rely = self.room.roomman.room.relativey
	dd = self.depth-1 
	## SE dagonal orientation
	if rely == relx:### or rely == relx-270:
			####FIXME node.z
			###if node.collide == 1:
		print "++++++++++++++++++++++++++++++collision in walkin..."
		self.result = 1	
		return
	if node.isleaf():
		print "leaf!"
		return
	for i in range(0,21):
		if node.nodes.getwithindex(i) != None:
			relx = self.room.roomman.room.relativex
			rely = self.room.roomman.room.relativey
			###print "px=%d py=%d to relx=%d rely=%d\n" % (px, py,relx,rely)
			if px + self.room.roomman.room.relativex < node.nodes.getwithindex(i).x*(depth-1):
				self.walkinrec(depth-1,node.nodes.getwithindex(i),player)
   
    def draw(self,player,screen,rotx,roty,rotz):
	self.result = 0
	self.drawrec(self.depth,self.tree,player,rotx,roty,rotz)
##	a = pygame.surfarray.pixels3d(pygame.display.get_surface())
##	e = engine(rotx,roty,rotz)
##	relx = self.room.roomman.room.relativex
##	rely = self.room.roomman.room.relativey

##	for i in range(0,480):
##		for j in range(0,640):
##			c = a[j][i]
##			color = (c[0],c[1],c[2])
##			r1 = Vector3(j,i,self.depth*10)
##			if rotx:
##				r1 = e.mx.multiply(r1)
##			if roty:
##				r1 = e.my.multiply(r1)
##			if rotz:
##				r1 = e.mz.multiply(r1)
##			r1x = r1.array[0]	
##			r1y = r1.array[1]	
##			r1z = r1.array[2]	
##			pygame.draw.line(pygame.display.get_surface(),color,(r1x+relx,r1y+rely),(r1x+relx,r1y+rely))
        ###screen.scr.blit(self.image, (0,0))
	return self.result

    def drawrec(self,depth,node,player,rotx,roty,rotz):
       	###FIXpygame.display.get_surface().blit(self.font.render("Loading...", 8, (255,255,255)), (140,320))
	###a = pygame.surfarray.pixels3d(self.image)###pygame.display.get_surface())
	px = player.x
	py = player.y
	pz = player.z
	relx = self.room.roomman.room.relativex
	rely = self.room.roomman.room.relativey
	dd = self.depth-1 
	## SE dagonal orientation
	###if depth <= 4 and not node.isleaf() and node.nodes.length > 0:###node.isleaf():### or not node.isleaf():
	if node.isleaf() and node.collide == 1:
		r1x = node.x	
		r1y = node.y 
		r1z = node.z
		r1w = node.w	
		r1h = node.h
		r1d = node.d
		###pygame.draw.line(pygame.display.get_surface(), (0,255,0), (r1x+relx,r1y+rely), (r1x+relx+r1w,r1y+rely))
		###pygame.draw.line(pygame.display.get_surface(), (0,255,0), (r1x+relx+r1w,r1y+rely), (r1x+relx+r1w,r1y+rely+r1h))
		###pygame.draw.line(pygame.display.get_surface(), (0,255,0), (r1x+relx+r1w,r1y+rely+r1h), (r1x+relx,r1y+rely+r1h))
		###pygame.draw.line(pygame.display.get_surface(), (0,255,0), (r1x+relx,r1y+rely+r1h), (r1x+relx,r1y+rely))

		for i in range(0,21):###FIXME1
			e = engine(rotx,roty,rotz)

			r1 = Vector3(node.x,node.y,node.z)
			r2 = Vector3(node.x,node.y,node.z)
			if rotx:
				r1 = e.mx.multiply(r1)
				r2 = e.mx.multiply(r2)
			if roty:
				r1 = e.my.multiply(r1)
				r2 = e.my.multiply(r2)
			if rotz:
				r1 = e.mz.multiply(r1)
				r2 = e.mz.multiply(r2)
			r1x = r1.array[0]	
			r1y = r1.array[1]	
			r1z = r1.array[2]	
			r2x = r2.array[0]	
			r2y = r2.array[1]	
			r2z = r2.array[2]
			color = (255,0,0)
			if node.collide:
				color = (0,0,255)
			pygame.draw.line(pygame.display.get_surface(), color, (r1x+relx,r1y+rely), (r2x+relx,r2y+rely))
			###pygame.draw.line(pygame.display.get_surface(), color, (r1x+relx+n1.w*(self.depth-1),r1y+rely+n1.h*(self.depth-1)), (r2x+relx+n2.w*(self.depth-1),r2y+rely+n2.h*(self.depth-1)))
			###pygame.draw.line(pygame.display.get_surface(), color, (r1x+relx+n1.w*(self.depth-1),r1y+rely+n1.h*(self.depth-1)), (r2x+relx+n2.w*(self.depth-1),r2y+rely+n2.h*(self.depth-1)))
			###pygame.draw.line(pygame.display.get_surface(), color, (r1x+relx,r1y+rely), (r2x+relx+n2.w*(self.depth-1),r2y+rely+n2.h*(self.depth-1)))
			###pygame.draw.line(pygame.display.get_surface(), color, (r1x+relx+n1.w*(self.depth-1),r1y+rely+n1.h*(self.depth-1)), (r2x+relx,r2y+rely))
	###if depth <= 0: 
	###	return
       		pygame.display.get_surface().blit(self.image, (r1x+relx,r1y+rely))
	##for j in range(0,node.w):
	##		for i in range(0,node.h):
	##			print "--->%d" % (i)
	##			c = a[i%24][j%24]
	##			color = (c[0],c[1],c[2])	
	##			pygame.draw.line(pygame.display.get_surface(),color,(r1x+i+relx,r1y+j+rely),(r1x+i+relx,r1y+j+rely))

	for i in range(0,21):
		if not node.isleaf() and node.nodes.getwithindex(i) != None:
			self.drawrec(depth-1,node.nodes.getwithindex(i),player,rotx,roty,rotz)

    def draw2(self,player,screen):
	self.result = 0
	self.drawrec2(self.depth,self.tree,player)
	return self.result

    def drawrec2(self,depth,node,player):
	px = player.x
	py = player.y
	pz = player.z
	relx = self.room.roomman.room.relativex
	rely = self.room.roomman.room.relativey
	dd = self.depth-1 
	## SE dagonal orientation
	###if depth <= 4 and not node.isleaf() and node.nodes.length > 0:###node.isleaf():### or not node.isleaf():
	if node.isleaf():
		r1x = node.x	
		r1y = node.y 
		r1z = node.z
		r1w = node.w	
		r1h = node.h
		r1d = node.d
		pygame.draw.line(pygame.display.get_surface(), (255,0,0), (r1x+relx,r1y+rely), (r1x+relx+r1w,r1y+rely))
		pygame.draw.line(pygame.display.get_surface(), (255,0,0), (r1x+relx+r1w,r1y+rely), (r1x+relx+r1w,r1y+rely+r1h))
		pygame.draw.line(pygame.display.get_surface(), (255,0,0), (r1x+relx+r1w,r1y+rely+r1h), (r1x+relx,r1y+rely+r1h))
		pygame.draw.line(pygame.display.get_surface(), (255,0,0), (r1x+relx,r1y+rely+r1h), (r1x+relx,r1y+rely))

	for i in range(0,21):
		if not node.isleaf() and node.nodes and node.nodes.length > 0 and node.nodes.getwithindex(i) != None:
			self.drawrec2(depth-1,node.nodes.getwithindex(i),player)
    

    def draw3(self,player,screen):
	self.result = 0
	self.drawrec3(self.depth,self.tree,player)
	return self.result


    def drawrec3(self,depth,node,player,rotx,roty,rotz):
	px = player.x
	py = player.y
	pz = player.z
	relx = self.room.roomman.room.relativex
	rely = self.room.roomman.room.relativey
	dd = self.depth-1 
	## SE dagonal orientation
	###if depth <= 4 and not node.isleaf() and node.nodes.length > 0:###node.isleaf():### or not node.isleaf():
	if depth <= 4 and not node.isleaf() and node.nodes.length > 17:###node.isleaf():### or not node.isleaf():
		print "leaf!"
		for i in range(0,21):###FIXME1
			n2 = node.nodes.getwithindex(i)
			for j in range(0,21):
				n1 = node.nodes.getwithindex(j)
				e = engine(rotx,roty,rotz)

				r1 = Vector3(n1.x,n1.y,n1.z)
				r2 = Vector3(n2.x,n2.y,n2.z)
				if rotx:
					r1 = e.mx.multiply(r1)
					r2 = e.mx.multiply(r2)
				if roty:
					r1 = e.my.multiply(r1)
					r2 = e.my.multiply(r2)
				if rotz:
					r1 = e.mz.multiply(r1)
					r2 = e.mz.multiply(r2)
				r1x = r1.array[0]	
				r1y = r1.array[1]	
				r1z = r1.array[2]	
				r2x = r2.array[0]	
				r2y = r2.array[1]	
				r2z = r2.array[2]
				color = (255,0,0)
				if n2.collide or n1.collide:
					color = (0,0,255)
				##pygame.draw.line(pygame.display.get_surface(), color, (r1x+relx,r1y+rely), (r2x+relx,r2y+rely))
				##pygame.draw.line(pygame.display.get_surface(), color, (r1x+relx+n1.w*(self.depth-1),r1y+rely+n1.h*(self.depth-1)), (r2x+relx+n2.w*(self.depth-1),r2y+rely+n2.h*(self.depth-1)))
				##pygame.draw.line(pygame.display.get_surface(), color, (r1x+relx,r1y+rely), (r2x+relx+n2.w*(self.depth-1),r2y+rely+n2.h*(self.depth-1)))
				##pygame.draw.line(pygame.display.get_surface(), color, (r1x+relx+n1.w*(self.depth-1),r1y+rely+n1.h*(self.depth-1)), (r2x+relx,r2y+rely))
		###if depth <= 0: 
		###	return

	if node.isleaf():
		r1x = node.x	
		r1y = node.y 
		r1z = node.z
		r1w = node.w	
		r1h = node.h
		r1d = node.d
		pygame.draw.line(pygame.display.get_surface(), (0,255,0), (r1x+relx,r1y+rely), (r1x+relx+r1w,r1y+rely))
		pygame.draw.line(pygame.display.get_surface(), (0,255,0), (r1x+relx+r1w,r1y+rely), (r1x+relx+r1w,r1y+rely+r1h))
		pygame.draw.line(pygame.display.get_surface(), (0,255,0), (r1x+relx+r1w,r1y+rely+r1h), (r1x+relx,r1y+rely+r1h))
		pygame.draw.line(pygame.display.get_surface(), (0,255,0), (r1x+relx,r1y+rely+r1h), (r1x+relx,r1y+rely))
		a = pygame.surfarray.pixels3d(self.image)###pygame.display.get_surface())

		for j in range(0,24):
			for i in range(0,24):
				print "--->%d" % (i)
				c = a[i][j]
				color = (c[0],c[1],c[2])	
				pygame.draw.line(pygame.display.get_surface(),color,(r1x+i+relx,r1y+j+rely),(r1x+i+relx,r1y+j+rely))

	for i in range(0,21):
		### if not node.isleaf() and node.nodes and node.nodes.length > 0 and node.nodes.getwithindex(i) != None:
		if not node.isleaf() and node.nodes.getwithindex(i) != None:
			###relx = self.room.roomman.room.relativex
			###rely = self.room.roomman.room.relativey
			###print "px=%d py=%d to relx=%d rely=%d\n" % (px, py,relx,rely)
			##if px + self.room.roomman.room.relativex < node.nodes.getwithindex(i).x*(depth-1):
			self.drawrec3(depth-1,node.nodes.getwithindex(i),player,rotx,roty,rotz)
	

# Copyright (C) Johan Ceuppens 2011
# Copyright (C) Johan Ceuppens 2010

# Copyright (C) Johan Ceuppens 2009 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import pygame
from pygame.locals import *

class Player:
    "Player"
    def __init__(self):
	#Entity.__init__(self,200,210,50,50)
        self.x = 40#400
        self.y = 210#100
        self.z = 110#100
        self.w = 50
        self.h = 50
        self.d = 50
        self.depth = 0
        self.direction = "south"
        self.images = []
        self.images2 = []
        self.images3 = []
        self.images4 = []

        self.image1 = pygame.image.load('./pics/rogueassassinleft1.bmp').convert()
        self.image2 = pygame.image.load('./pics/rogueassassinleft2.bmp').convert()
        self.image3 = pygame.image.load('./pics/rogueassassinleft3.bmp').convert()
        self.image4 = pygame.image.load('./pics/rogueassassinleft2.bmp').convert()
        self.image1.set_colorkey((0,0,0)) 
        self.image2.set_colorkey((0,0,0)) 
        self.image3.set_colorkey((0,0,0)) 
        self.image4.set_colorkey((0,0,0)) 

        self.image5 = pygame.image.load('./pics/rogueassassinright1.bmp').convert()
        self.image6 = pygame.image.load('./pics/rogueassassinright2.bmp').convert()
        self.image7 = pygame.image.load('./pics/rogueassassinright3.bmp').convert()
        self.image8 = pygame.image.load('./pics/rogueassassinright2.bmp').convert()
        self.image5.set_colorkey((0,0,0)) 
        self.image6.set_colorkey((0,0,0)) 
        self.image7.set_colorkey((0,0,0)) 
        self.image8.set_colorkey((0,0,0)) 

        self.image9 = pygame.image.load('./pics/rogueassassinup1.bmp').convert()
        self.image10 = pygame.image.load('./pics/rogueassassinup2.bmp').convert()
        self.image11 = pygame.image.load('./pics/rogueassassinup3.bmp').convert()
        self.image12 = pygame.image.load('./pics/rogueassassinup2.bmp').convert()
        self.image9.set_colorkey((0,0,0)) 
        self.image10.set_colorkey((0,0,0)) 
        self.image11.set_colorkey((0,0,0)) 
        self.image12.set_colorkey((0,0,0)) 
 
        self.image13 = pygame.image.load('./pics/rogueassassindown1.bmp').convert()
        self.image14 = pygame.image.load('./pics/rogueassassindown2.bmp').convert()
        self.image15 = pygame.image.load('./pics/rogueassassindown3.bmp').convert()
        self.image16 = pygame.image.load('./pics/rogueassassindown2.bmp').convert()
        self.image13.set_colorkey((0,0,0)) 
        self.image14.set_colorkey((0,0,0)) 
        self.image15.set_colorkey((0,0,0)) 
        self.image16.set_colorkey((0,0,0)) 

        self.imageindex = 0
        self.addimages()

        self.talkcounter = 0
        self.currenttext = ""
    
    def addimages(self):
        self.images.append(self.image1)
        self.images.append(self.image2)
        self.images.append(self.image3)
        self.images.append(self.image4)
        self.images2.append(self.image5)
        self.images2.append(self.image6)
        self.images2.append(self.image7)
        self.images2.append(self.image8)
        self.images3.append(self.image9)
        self.images3.append(self.image10)
        self.images3.append(self.image11)
        self.images3.append(self.image12)
        self.images4.append(self.image13)
        self.images4.append(self.image14)
        self.images4.append(self.image15)
        self.images4.append(self.image16)

    def warp(self,x,y):
        self.x = x
        self.y = y


    def moveupanddirect(self,level):
        level.roommanager.room.moveup(self)

    def movedownanddirect(self,level):
        level.roommanager.room.movedown(self)

    def moveleftanddirect(self,level):
        level.roommanager.room.moveleft(self)

    def moverightanddirect(self,level):
        level.roommanager.room.moveright(self)


    def moveupanddirect1(self):
        self.direction = "north"
        self.y = self.y - 12
        self.imageindex = self.imageindex + 1

    def movedownanddirect1(self):
        self.direction = "south"
        self.y = self.y + 12
        self.imageindex = self.imageindex + 1

    def moveleftanddirect1(self):
        self.direction = "west"
        self.x = self.x - 12
        self.imageindex = self.imageindex + 1

    def moverightanddirect1(self):
        self.direction = "east"
        self.x = self.x + 12
        self.imageindex = self.imageindex + 1

    def moveup(self):
        self.y = self.y - 12

    def movedown(self):
        self.y = self.y + 12

    def moveleft(self):
        self.x = self.x - 12

    def moveright(self):
        self.x = self.x + 12


    def bounceback(self,level):
        if self.direction == "north":
            self.movedownanddirect(level)
            #self.movedown()
        elif self.direction == "south":
            self.moveupanddirect(level)
            #self.moveup()
        elif self.direction == "west":
            self.moverightanddirect(level)
            #self.moveright()
        elif self.direction == "east":
            self.moveleftanddirect(level)
            #self.moveleft()


    def draw(self):
        self.imageindex = self.imageindex + 1
        if self.imageindex > 3:
            self.imageindex = 0

	if self.direction == "west":
            return self.images[self.imageindex]
        elif self.direction == "east": 
            return self.images2[self.imageindex]
	elif self.direction == "north":
            return self.images3[self.imageindex]
        elif self.direction == "south": 
            return self.images4[self.imageindex]

    def draw0(self):
        self.imageindex = 0
	if self.direction == "west":
            return self.images[0]
        elif self.direction == "east": 
            return self.images2[0]
	elif self.direction == "north":
            return self.images3[0]
        elif self.direction == "south": 
            return self.images4[0]

    def warp(self,x,y):
        self.x = x
        self.y = y


    def moveupanddirect(self,level):
        level.roommanager.room.moveup(self)

    def movedownanddirect(self,level):
        level.roommanager.room.movedown(self)

    def moveleftanddirect(self,level):
        level.roommanager.room.moveleft(self)

    def moverightanddirect(self,level):
        level.roommanager.room.moveright(self)


    def moveupanddirect1(self):
        self.direction = "north"
        self.y = self.y - 12
        self.imageindex = self.imageindex + 1

    def movedownanddirect1(self):
        self.direction = "south"
        self.y = self.y + 12
        self.imageindex = self.imageindex + 1

    def moveleftanddirect1(self):
        self.direction = "west"
        self.x = self.x - 12
        self.imageindex = self.imageindex + 1

    def moverightanddirect1(self):
        self.direction = "east"
        self.x = self.x + 12
        self.imageindex = self.imageindex + 1

    def moveup(self):
        self.y = self.y - 12

    def movedown(self):
        self.y = self.y + 12

    def moveleft(self):
        self.x = self.x - 12

    def moveright(self):
        self.x = self.x + 12


    def decreasehp(self,level):
	print ("player is hit!")
        self.hitpoints -= self.decreasehitpoints

        if self.hitpoints <= 0:
            level.setgameover()
    
    def increasehp(self):
	if self.hitpoints < 30:
            self.hitpoints += 5 

    def collisionplayer(self,mouse):
        if mouse.x >= self.x and mouse.x <= self.x + self.w and mouse.y >= self.y and mouse.y <= self.y + self.h:   	
              return self
        return None

    def processorrelativex(self,o,relx):
        return o.x - relx

    def processorrelativey(self,o,rely):
        return o.y - rely

    def collisionplayerrelative(self,mouse,relx,rely):
        px = self.processorrelativex(self,relx)
        py = self.processorrelativey(self,rely)

        x = self.processorrelativex(mouse, relx)
        y = self.processorrelativey(mouse, rely)
        if x >= px and x <= px + self.w and y >= py and y <= py + self.h:   	
              return self
        return None

    def collisionlist(self,list):
        return self.collisionlist1(self,list)

    def collisionlistblocks(self,list):
        return self.collisionlistblocks1(self,list)

    # GEN PROC
    def collisionlist1(self,collider,list):
        for i in range(0,list.length):
            o = None
            o = list.getlistobject(o)
            
            if o:
#                print 'i=%d x=%d y=%d px=%d py=%d' % (i, o.x,o.y,collider.x,collider.y)
                if collider.x >= o.x and collider.x <= o.x + o.w and collider.y >= o.y and collider.y <= o.y + o.h:   	
                    return o
        return None

    # GEN PROC
    def collisionlistblocks1(self,collider,list):
        for i in range(0,list.length):
            o = None
            o = list.getlistobject(o)
            
            if o:
#                print 'i=%d x=%d y=%d px=%d py=%d' % (i, o.x,o.y,collider.x,collider.y)
                if collider.x >= o.x and collider.x <= o.x + o.w and collider.y >= o.y and collider.y <= o.y + o.h and collider.z <= o.z + o.d and collider.z >= o.z:   	
                    return o
        return None

	# SWORD COLLISON
    def collisionlist2(self,collider,list):
        for i in range(0,list.length):
            o = None
            o = list.getlistobject(o)
            
            if o:
#                print 'i=%d x=%d y=%d px=%d py=%d' % (i, o.x,o.y,collider.x,collider.y)
                if collider.x+30 >= o.x and collider.x <= o.x + o.w+30 and collider.y +30>= o.y and collider.y <= o.y + o.h +30:   	
                    return o
        return None

    #FIXME put in Utitlities
    def processorrelativex(self,o,relx):
        return o.x - relx

    def processorrelativey(self,o,rely):
        return o.y - rely
    
    def collisionlistrelative(self,list,relx,rely):
        return self.collisionlistrelative1(self,list,relx,rely)

    # GEN PROC
    def collisionlistrelative1(self,collider,list,relx,rely):
        for i in range(0,list.length):
            o = None
            o = list.getlistobject(o)

            if o:
       
                #FIXME1 o.x <-> self.relativex
                x = o.x - 12#self.processorrelativex(o,relx)
                y = o.y - 12#self.processorrelativey(o,rely)
                px = self.processorrelativex(self,relx)
                py = self.processorrelativey(self,rely)

#                print 'i=%d x=%d y=%d px=%d py=%d' % (i, o.x,o.y,px,py)
		# NOTE : +24 for better sword collision
                if px + 12 >= x and px <= x + o.w + 12 and py + 12 >= y and py <= y + o.h + 12:   	
                    return o
        return None

    def leftbuttonpressed(self,game):
        if game.mouse.press(0) == 1:
            self.talkcounter += 1
            #print "keypress on player"
            if self.talkcounter == 1:
                1#self.currenttext = self.talksystem.gettalk(text)#foo
            return self.currenttext
        else:
            self.talkcounter = 0 	
            return self.currenttext

    def jump(self,game,ascend):
        if game.level.roommanager.room.collisionboxes(self) or game.level.roommanager.room.octtree.collision(self,game):
		if ascend:
			self.depth -= 1
		else:
			self.depth += 1
	if self.direction == "north":
###		if ascend:
		self.moveupanddirect(game.level)
		self.moveupanddirect(game.level)
		self.moveupanddirect(game.level)
		self.moveupanddirect(game.level)
		self.moveupanddirect(game.level)
		self.moveupanddirect(game.level)
###		else:
###			self.movedownanddirect(game.level)
###			self.movedownanddirect(game.level)
###			self.movedownanddirect(game.level)
###			self.movedownanddirect(game.level)
###			self.movedownanddirect(game.level)
###			self.movedownanddirect(game.level)
	if self.direction == "south":
		self.movedownanddirect(game.level)
		self.movedownanddirect(game.level)
		self.movedownanddirect(game.level)
		self.movedownanddirect(game.level)
		self.movedownanddirect(game.level)
		self.movedownanddirect(game.level)
		self.movedownanddirect(game.level)
		self.movedownanddirect(game.level)
		self.movedownanddirect(game.level)
		self.movedownanddirect(game.level)
		self.movedownanddirect(game.level)
		self.movedownanddirect(game.level)
	if self.direction == "west":
		self.moveleftanddirect(game.level)
		self.moveleftanddirect(game.level)
		self.moveleftanddirect(game.level)
		self.moveleftanddirect(game.level)
		self.moveleftanddirect(game.level)
		self.moveleftanddirect(game.level)
	if self.direction == "east":
		self.moverightanddirect(game.level)
		self.moverightanddirect(game.level)
		self.moverightanddirect(game.level)
		self.moverightanddirect(game.level)
		self.moverightanddirect(game.level)
		self.moverightanddirect(game.level)
	###else:
	###	self.depth = 0 

# Copyright (C) Johan Ceuppens 2011
# Copyright (C) Johan Ceuppens 2010

# Copyright (C) Johan Ceuppens 2009 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import pygame
from pygame.locals import *

#from key import *
#from lockeddoornorth import *
#from lockeddoorsouth import *
#from lockeddooreast import *
#from lockeddoorwest import *

from world import *
#from mapworld import *

class Roommanager:
    "Room manager"
    def __init__(self, screen, player,font):
	self.screen = screen
        self.room = None
	self.player = player
	self.font = font
        self.load(Room2world(self))
###        self.load(Room1world(self))
###        self.load(DungeonundertreeCastle(-100,-100,self))
#        self.load(Worldmapinsidetemple5(-100,-300,None))
#        self.load(Dungeonwaterlower2(None))
#        self.load(Dungeonwater5(-200,-100,None))
#        self.load(Roomdungeonwatercherubs(None))
#        self.load(Roombirdforest(-20,-200,None))
#        self.load(Worldmaproom3(-1000,-100, None))
        # other entries in the game :

#        self.load(Room5dungeon1())

#        self.load(Room9dungeon2())
#        self.load(Worldmaproom1(0,-520))
     
    def load(self,room):
        self.room = room.loadroom()

    def getroom(self):
        return self.room
    

# Copyright (C) Johan Ceuppens 2011
# Copyright (C) Johan Ceuppens 2010

# Copyright (C) Johan Ceuppens 2009 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from list import *
from box import *
from block import *
from exitbox import *

from utility import *

from entityfactory import *

import inspect

class Room:
    ""
    def __init__(self):
        self.initroom()
        self.entityfactory = None

    # FIXME
    def addfactory(self,factory):
        frame = inspect.currentframe()
        try:
            frame != None
        finally:
            factory.add(self)

    def loadroom(self,roommanager):
         roommanager.loadroom(self)

    def updatelist(self,list):
        for el in list.list:
            if el:
                el.update(self)

    def addbox(self,x,y,w,h):
        self.boxes.add(Box(x,y,w,h))

    def addblock(self,x,y,z,w,h,d):
        self.blocks.add(Block(x,y,z,w,h,d))

    def addexit(self,x,y,w,h,roomid,newx,newy,room):
         self.exits.add(Exitbox(x,y,w,h,roomid,newx,newy,room))

    def addentitythroughfactory(self,room):
        1#FIXME

    def addentity(self,entity):
        if entity:
            self.entities.add(entity)

    def addenemy(self,enemy):
        self.addentity(enemy)

    def addsprite(self,sprite):
        self.addentity(sprite)

    def loadwalls(self):
        1

    def initroom(self):
	print "initroom"
        self.totalboxes = 0
        self.boxes = List()
        self.blocks = List()
        self.totalexits = 0
        self.exits = List()
        self.background = None
        self.entities = List()
        self.idcounter = 0
        self.night = 0
        self.loadwalls()
	

    def draw(self,screen):
        screen.scr.blit(self.background, (0, 0))
        screen.draw(self.entities)

#    def auxdraw(self):
#	self.auxdraw(self.)
#        screen.scr.blit(self.background, (0, 0))
#        screen.draw(self.entities)

    def update(self,player,level,swordplay):#unsused level
        self.updatelist(self.entities)

        o = player.collisionlist(self.entities)
	if o:
	    #o.dofood(player)
	    o.remove(self.entities)
 
    def collisionplayer(self,mouse,player):
        mouse.collisionplayer(player)

    def domouse(self,game):
        # Collide mouse with player

        p = game.domouseonplayer(self)
	if p:
            print("mouse click on player 1")
            foo = game.leftbuttonpressed(game.player)
            if foo:
                print("mouse click on player 2")
                return
            
        # Collide mouse with entities
        o = None
        o = game.collisionlistonmouse(self.entities)
	#print self.entities.getwithindex(0)
        if o:
            foo = game.leftbuttonpressed(o)
#            print foo
            if foo:
                game.screen.scr.blit(game.font.render(foo, 16, (255,255,255)), (10,10))
#                print foo
            else:
                game.screen.scr.blit(game.font.render(o.getname(game.text), 10, (255,255,255)), (game.mouse.x+10,game.mouse.y))
#            mouse.middlebuttonpressed(o,text)
#            mouse.rightbuttonpressed(o,text)
            


    def domouseinventory(self,game):#,screen,mouse,font,text,item,player):
        # Collide mouse with player
        p = game.domouseonplayer(self)#game.mouse.collisionplayer(game)
        if p:
            foo = game.mouse.leftbuttonpressedwithinventoryitem(game.player,game)
            if foo:
                return foo 

        # Collide mouse with entities
        o = None
        o = game.collisionlistonmouse(self.entities)
        if o and game.inventoryitem:
            foo = game.mouse.leftbuttonpressedwithinventoryitem(o,game)#.text,game.inventoryitem)
            if foo:
                return foo
        if not o and game.mouse.press(0):
            return None#1

        return None
#             if foo:
#                 screen.scr.blit(font.render(foo, 16, (255,255,255)), (10,10))
# #                print foo
#             else:
#                 screen.scr.blit(font.render(o.getname(text), 10, (0,0,0)), (mouse.x+10,mouse.y))
#            mouse.middlebuttonpressed(o,text)
#            mouse.rightbuttonpressed(o,text)



    def moveup(self, player):
        player.moveupanddirect1()

    def movedown(self, player):
        player.movedownanddirect1()

    def moveleft(self, player):
        player.moveleftanddirect1()

    def moveright(self, player):
        player.moverightanddirect1()


######## COLLISION CODE

    def collisionlistbomb(self,bomb,list):
#         for i in range(0,list.length):
#             o = None
#             o = list.getlistobject(o)

        for o in list.list:
            if o and o.x +12>= bomb.x - bomb.w and o.x <= bomb.x + bomb.w +12and o.y+12 >= bomb.y - bomb.h and o.y <= bomb.y + bomb.h +12:
                return o
        return None


    def collisionexits(self,player,roommanager):
        o = player.collisionlist(self.exits)

        if o:

            print 'foo x=%d,y=%d px=%d py=%d' % (o.x,o.y,player.x,player.y)

            o.room.loadroom()
            roommanager.room = o.room
            player.warp(o.newx,o.newy)
            return 1
        return None

    def collisionboxes(self,player):
        o = player.collisionlist(self.boxes)
        
        if o:
            #player.bounceback()#FIXME1 level arg
            return 1
        return None

    def collisionblocks(self,player):
        o = player.collisionlistblocks(self.blocks)
        
        if o:
            #player.bounceback()#FIXME1 level arg
            return 1
        return None


    def collisionandremoveenemieswithbomb(self, bomb):
        o = self.collisionlistbomb(bomb,self.entities)
        if o:
            o.hit()
            o.remove(self.entities)
            self.entities.remove(o)
            
    def summonskeletonunderplayercommand(self,x,y):
        self.addenemy(Skeletonunderplayercommand(x,y,50,50))


    def summonskeleton(self,x,y):
        self.addenemy(Skeleton2(x,y,50,50))

    def removeallenemies(self):
        self.entities = List()
            
    def removeallentities(self):
        self.entities = List()



# Copyright (C) Johan Ceuppens 2011
# Copyright (C) Johan Ceuppens 2010

# Copyright (C) Johan Ceuppens 2009 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from list import *
from box import *
from exitbox import *
from room import *
from utility import *

from entityfactory import *

import inspect

class Roomroomman(Room):
    ""
    def __init__(self, roomman):
        Room.__init__(self)
        self.initroom()
        self.entityfactory = None
	self.roomman = roomman

    # FIXME
    def addfactory(self,factory):
        frame = inspect.currentframe()
        try:
            frame != None
        finally:
            factory.add(self)

    def loadroom(self,roommanager):
         roommanager.loadroom(self)

    def updatelist(self,list):
        for el in list.list:
            if el:
                el.update(self)

    def addbox(self,x,y,w,h):
        self.boxes.add(Box(x,y,w,h))

    def addexit(self,x,y,w,h,roomid,newx,newy,room):
         self.exits.add(Exitbox(x,y,w,h,roomid,newx,newy,room))

    def addentitythroughfactory(self,room):
        1#FIXME

    def addentity(self,entity):
        if entity:
            self.entities.add(entity)

    def addenemy(self,enemy):
        self.addentity(enemy)

    def addsprite(self,sprite):
        self.addentity(sprite)

    def loadwalls(self):
        1

    def initroom(self):
        self.totalboxes = 0
        self.boxes = List()
        self.totalexits = 0
        self.exits = List()
        self.background = None
        self.entities = List()
        self.idcounter = 0
        self.night = 0
        self.loadwalls()
	

    def draw(self,screen):
        screen.scr.blit(self.background, (0, 0))
        screen.draw(self.entities)

#    def auxdraw(self):
#	self.auxdraw(self.)
#        screen.scr.blit(self.background, (0, 0))
#        screen.draw(self.entities)

    def update(self,player,level,swordplay):#unsused level
        self.updatelist(self.entities)

        o = player.collisionlist(self.entities)
        if o:
#	    print ("%d" % (o.platform))
	    if swordplay != 0:
		print ("--------SWORDPLAY")
		o.hitpoints -= 1
        	if o.hitpoints <= 0:
            	    self.entities.remove(o)    
	    else:
	        o.dofood(player) 
	        o.doplatform(player,level) 
                player.bounceback(level)
                o.decreasehpinplayer(player, level)
	    o.remove(self.entities)
 
    def collisionplayer(self,mouse,player):
        mouse.collisionplayer(player)

    def domouse(self,game):
        # Collide mouse with player

        p = game.domouseonplayer(self)
        if p:
            foo = game.leftbuttonpressed(game.player)
            if foo:
                return
            
        # Collide mouse with entities
        o = None
        o = game.collisionlistonmouse(self.entities)
        if o:
            foo = game.leftbuttonpressed(o)
#            print foo
            if foo:
                game.screen.scr.blit(game.font.render(foo, 16, (255,255,255)), (10,10))
#                print foo
            else:
                game.screen.scr.blit(game.font.render(o.getname(game.text), 10, (0,0,0)), (game.mouse.x+10,game.mouse.y))
#            mouse.middlebuttonpressed(o,text)
#            mouse.rightbuttonpressed(o,text)
            


    def domouseinventory(self,game):#,screen,mouse,font,text,item,player):
        # Collide mouse with player
        p = game.domouseonplayer(self)#game.mouse.collisionplayer(game)
        if p:
            foo = game.mouse.leftbuttonpressedwithinventoryitem(game.player,game)
            if foo:
                return foo 

        # Collide mouse with entities
        o = None
        o = game.collisionlistonmouse(self.entities)
        if o and game.inventoryitem:
            foo = game.mouse.leftbuttonpressedwithinventoryitem(o,game)#.text,game.inventoryitem)
            if foo:
                return 1

        if not o and game.mouse.press(0):
            return 1

        return None
#             if foo:
#                 screen.scr.blit(font.render(foo, 16, (255,255,255)), (10,10))
# #                print foo
#             else:
#                 screen.scr.blit(font.render(o.getname(text), 10, (0,0,0)), (mouse.x+10,mouse.y))
#            mouse.middlebuttonpressed(o,text)
#            mouse.rightbuttonpressed(o,text)



    def moveup(self, player):
        player.moveupanddirect1()

    def movedown(self, player):
        player.movedownanddirect1()

    def moveleft(self, player):
        player.moveleftanddirect1()

    def moveright(self, player):
        player.moverightanddirect1()


######## COLLISION CODE

    def collisionlistbomb(self,bomb,list):
#         for i in range(0,list.length):
#             o = None
#             o = list.getlistobject(o)

        for o in list.list:
            if o and o.x >= bomb.x - bomb.w and o.x <= bomb.x + bomb.w and o.y >= bomb.y - bomb.h and o.y <= bomb.y + bomb.h:
                return o
        return None


    def collisionexits(self,player,roommanager):
        o = player.collisionlist(self.exits)

        if o:

            print 'foo x=%d,y=%d px=%d py=%d' % (o.x,o.y,player.x,player.y)

            o.room.loadroom()
            roommanager.room = o.room
            player.warp(o.newx,o.newy)
            return 1
        return None

    def collisionboxes(self,player):
        o = player.collisionlist(self.boxes)
        
        if o:
            #player.bounceback()#FIXME1 level arg
            return 1
        return None


    def collisionandremoveenemieswithbomb(self, bomb):
        o = self.collisionlistbomb(bomb,self.entities)
        if o:
            o.hit()
            o.remove(self.entities)
            self.entities.remove(o)
            
    def summonskeletonunderplayercommand(self,x,y):
        self.addenemy(Skeletonunderplayercommand(x,y,50,50))


    def summonskeleton(self,x,y):
        self.addenemy(Skeleton2(x,y,50,50))

    def removeallenemies(self):
        self.entities = List()
            
    def removeallentities(self):
        self.entities = List()



# Copyright (C) Johan Ceuppens 2011
# Copyright (C) Johan Ceuppens 2010

# Copyright (C) Johan Ceuppens 2009 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import pygame
from pygame.locals import *

class Screen:
    def __init__(self):
        self.scr = pygame.display.set_mode((640, 480))

    def draw(self, list):
        for o in list.list:
            o.draw(self)

    def drawonmap(self, maproom):
        for o in maproom.entities.list:
            o.drawonmap(self, maproom)

# Copyright (C) Johan Ceuppens 2011
# Copyright (C) Johan Ceuppens 2010

# Copyright (C) Johan Ceuppens 2009 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import pygame
from pygame.locals import *

from entity import *

from random import *
import os

from utility import *

class Scrollinvisibility(Entity):
    def __init__(self,x,y,w,h,dungeonnumber,keynumber):
        Entity.__init__(self,x,y,w,h)
        self.addimage('./pics/scroll1.bmp',255,255,255)

        self.counter = 0
        self.dungeonnumber = dungeonnumber
        self.keynumber = keynumber

    def update(self,level):
        1

    def deathupdate(self):
        1
        
    #FIXME copy-paste in dracolich.py
    def remove(self,list):
        string = "You have a scroll of invisibility "
        string += self.keynumber
        string += " - dungeon "
        string += self.dungeonnumber
        string += "\n"

        s = ""

        if Utilities().readandfindstring(string):
            list.remove(self)
            return None
        
        f = open("./workfile",'r')
        for line in f:
            s += line

        f = open("./workfile",'w')
        
        f.write(s)
        f.write(string)
        f.write("\n")

        return 1
 
    #FIXME copy-paste in dracolich.py readboss
    def readkeys(self, list):
        string = "You have a scroll of invisibility "
        string += self.keynumber
        string += " - dungeon "
        string += self.dungeonnumber
        string += "\n"

        if Utilities().readandfindstring(string):
            print "+++ Scroll of invisibility registered"
            if list:
                list.remove(self)
            return 1

        return None

    def getname(self,text):
        return text.getscrollinvisibilityname()

# Copyright (C) Johan Ceuppens 2011
# Copyright (C) Johan Ceuppens 2010

# Copyright (C) Johan Ceuppens 2009 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


from talksystem import *
from graphbombman import *

class Talksystembombman(Talksystem):
    def __init__(self):
        Talksystem.__init__(self)
        self.graph = Graphbombman()
        
    def gettalk(self,text):
        return self.graph.get(text)
        

# Copyright (C) Johan Ceuppens 2011
# Copyright (C) Johan Ceuppens 2010

# Copyright (C) Johan Ceuppens 2009 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


from talksystem import *
from graphcapricornguardian3 import *

class Talksystemcapricornguardian3(Talksystem):
    def __init__(self):
        Talksystem.__init__(self)
        self.graph = Graphcapricornguardian3()
        
    def gettalk(self,text):
        return self.graph.get(text)
        

# Copyright (C) Johan Ceuppens 2011
# Copyright (C) Johan Ceuppens 2010

# Copyright (C) Johan Ceuppens 2009 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


from talksystem import *
from graphmyconid import *

class Talksystemmyconid(Talksystem):
    def __init__(self):
        Talksystem.__init__(self)
        self.graph = Graphmyconid()
        
    def gettalk(self,text):
        return self.graph.get(text)
        

# Copyright (C) Johan Ceuppens 2011
# Copyright (C) Johan Ceuppens 2010

# Copyright (C) Johan Ceuppens 2009 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


class Talksystem(object):
    def __init__(self):#pass
        1

    def gettalk(self,text):
        return None

# Copyright (C) Johan Ceuppens 2011
# Copyright (C) Johan Ceuppens 2010

# Copyright (C) Johan Ceuppens 2009 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


from text import *

class Textenglish(Text):


    def getname(self,o):
        return o.getnametext(self)

    def getnullname(self):
        return "..."
    
    def getbombmanname(self):
        return "Bomb Man"

    def getbombman1(self):
        return "Boom! Boom!"

    def getbombman2(self):
        return "Blast! Blast!"

    def getsword1bluename(self):
        return "Seraphim sword"

    def getcandlename(self):
        return "Candle"
    
    def getscrollinvisibilityname(self):
        return "Scroll of invisibility"
    
    def gethermitname(self):
        return "Hermit"
    
    def gettrollblackname(self):
        return "Black troll"

    def getoozebigname(self):
        return "Big blue ooze"

    def getoozename(self):
        return "Blue ooze"

    def gethoundarchonname(self):
        return "Hound archon"

    def getknightdarkname(self):
        return "Dark knight"

    def getmoneyname(self):
        return "Money"

    def getcapricornguardianname(self):
        return "Capricorn Guardian"

    def getcapricornguardian31(self):
        return "Do not fight Celestia!"

    def getcapricornguardian32(self):
        return "If you hurt me I will strike you!"

    def getsword1bluename(self):
        return "Seraphim sword"

    def getcandlename(self):
        return "Candle"
    
    def getscrollinvisibilityname(self):
        return "Scroll of invisibility"
    
    def gethermitname(self):
        return "Hermit"
    
    def gettrollblackname(self):
        return "Black troll"

    def getoozebigname(self):
        return "Big blue ooze"

    def getoozename(self):
        return "Blue ooze"

    def gethoundarchonname(self):
        return "Hound archon"

    def getknightdarkname(self):
        return "Dark knight"

    def getmoneyname(self):
        return "Money"

    def getfoodname(self):
        return "Food"

    def getchestname(self):
        return "Chest"

    def getunicornstatuename(self):
        return "Unicorn statue"

    def getringbluename(self):
        return "Blue ring"
    
    def getcherubstatuename(self):
        return "Cherub"
    
    def getbatname(self):
        return "Bat"
    
    def getcandleflame1name(self):
        return "Flame angel"

    def getmyconidbluename(self):
        return "Blue myconid"
    
    def getchaosknight1name(self):
        return "Dark Warrior"

    def getshadowbladename(self):
        return "Shadowblade"

    def getbirdsparrowname(self):
        return "Blue sparrow"

    def getnuttername(self):
        return "Nutter"

    def getnutter2name(self):
        return "Nutter"

    def getmyconidname(self):
        return "Myconid"
    
    def getshieldmaidenname(self):
        return "Elf"

    def getentname(self):
        return "Ent"

    def getskeletonname(self):
        return "Skeleton"

    def gettreename(self):
        return "Tree"

    def getblueorcname(self):
        return "Blue Orc"
    
    def gethobgoblinswordsmanname(self):
        return "Hobgoblin Swordsman"
    
    def getvillageorcmagename(self):
        return "Blue Orc Mage"

    def getorcpaladinname(self):
        return "Orc Paladin"
    
    def getorcswordsmanbluename(self):
        return "Orc swordsman"

    def getwaterdevilname(self):
        return "Water devil"

    def getwaterdevilfemalename(self):
        return "Water devil female"
    
    def getensorcelledtreename(self):
        return "Enscorcelled tree"

    def getangelname(self):
        return "Angel"
    
    def getdemon1name(self):
        return "Demon fighter"
    
    def getorchutname(self):
        return "Orc hut"
    
    def getearthweirdname(self):
        return "Earth weird"

    def getkeyname(self):
        return "Key"

    def getkey2name(self):
        return "Blue key"

    def getkey3name(self):
        return "Golden key"

    def getdracolichname(self):
        return "Dracolich"

    def getspellbookdracolichname(self):
        return "Dracolich's spellbook"

    def getskeletonofplayername(self):
        return "Skeleton under your command"

    def getkoboldmagename(self):
        return "Kobold mage"


    ############# NUTTER 

    def getnutter1(self):
        return "Hello. I am Stawie."

    def getnutter2(self):
        return "I am a disciple in magic. Nootie is my master."

    def getnutter3(self):
        return "He lives further north from here."

    def getnutter4(self):
        return "He is hard-hearing..so be patient with him."

    ############# NUTTER2 

    def getnutter21(self):
        return "We are nut people."

    def getnutter22(self):
        return "Green magic flows through us."


    ############# SPARROW 

    def getbirdsparrow1(self):
        return "There is a hole under the black tree."

    def getbirdsparrow2(self):
        return "All you have to do is remove the tree...haha!"

    #############  TROLL BLACKM

    def gettrollblack1(self):
        return "..gnuvy.."

    def gettrollblack2(self):
        return "*groink*"

    ############# HERMIT 

    def gethermit1(self):
        return "Use the Seraphim ring on walls."

    def gethermit2(self):
        return "You will be able to walk through."

    ############# CANDLEFLAME 2

    def getcandleflame21(self):
        return "This dungeon was a castle built on water."

    def getcandleflame22(self):
        return "We are the eternal flame which burns for this dungeon."

    def getcandleflame23(self):
        return "All elven lords of the castle went into these flames."

    def getcandleflame24(self):
        return "They are now one with the fire."

    def getcandleflame25(self):
        return "The fire contrasts the water, thus is our blessing."

    ############# CANDLEFLAME 1

    def getcandleflame11(self):
        return "We are fiery angels."

    def getcandleflame12(self):
        return "We are the Red Seraphim."

    def getcandleflame13(self):
        return "Wander into the flames if you must."

    def getcandleflame14(self):
        return "You will not be harmed."

    def getcandleflame15(self):
        return "That is our blessing."

    ############# DARK WARRIOR 

    def getchaosknight11(self):
        return "You will face defeat."

    def getchaosknight12(self):
        return "I will show no mercy."

    ############# MYCONID

    def getmyconid1(self):
        return "The earth is bright here."

    def getmyconid2(self):
        return "I'll learn you some magic."

    ############# EARTH WEIRD

    def getearthweird1(self):
        return "Hello."

    def getearthweird2(self):
        return "I'm an earth master."

    ############# KOBOLD MAGE

    def getkoboldmage1(self):
        return "Praise to Kurtulmak!"

    def getkoboldmage2(self):
        return "Bring me the secret of the dark."


    ############# ORCBLUE1 

    def getorcblue11(self):
        return "This is Staw .. Orc Village."

    def getorcblue12(self):
        return "We do not have many guests here."

    ############# VILLAGE ORC 2 

    def getvillageorc21(self):
        return "This is Staw .. Orc Village."

    def getvillageorc22(self):
        return "We do not have many guests here."

    ############# ORC VILLAGE 3 (not MAGE) 

    def getvillageorc31(self):
        return "I do not want to talk to you."

    def getvillageorc32(self):
        return "Go Away."

    ############# ORC VILLAGE 2 (MAGE) 

    def getvillageorc11(self):
        return "Kobold magi .. torture .. twilight .."

    def getvillageorc12(self):
        return "Do not travel to the north .. "

    ############# WATER DEVIL FEMALE 

    def getwaterdevilfemale1(self):
        return "Take the Elven sword."
    def getwaterdevilfemale2(self):
        return "It has a red Seraphim in it which guides you through the battle"
    def getwaterdevilfemale3(self):
        return "This water fortress was built by high elves"
    def getwaterdevilfemale4(self):
        return "Evil cursed the landlords and they are now hither"
    def getwaterdevilfemale5(self):
        return "Witch elves destroyed the keep"
    def getwaterdevilfemale6(self):
        return "I am the last elven maiden left"
    def getwaterdevilfemale7(self):
        return "I was condemned into this form"
    def getwaterdevilfemale8(self):
        return "My hope is with you."
    def getwaterdevilfemale9(self):
        return "Please defeat the evil which overtook us."
        return "You are our last hope."
    ############# WATER DEVIL FEMALE 

    def getwaterdevil1(self):
        return ".blurbs..."

    def getwaterdevil2(self):
        return "...splat.."

    ############# ORC SWORDSMAN

    def getorcswordsmanblue1(self):
        return "We were elves once."

    def getorcswordsmanblue2(self):
        return "But darkness took us..."

    ############# ORC WARRIOR VILLAGE

    def getorcpaladin1(self):
        return "I defend the good cause."

    def getorcpaladin2(self):
        return "May the gods be with you..."

    ############ SHIELD MAIDEN
    def getshieldmaiden1(self):
        return "These mountains connect the Elf Tower with the west."
    def getshieldmaiden2(self):
        return "The elves have gone now."
    def getshieldmaiden3(self):
        return "They left the tower ages ago."

    ############ HOBGOBLIN SWORDSMAN 1   
    def gethobgoblinswordsman1(self):
        return "..."
    def gethobgoblinswordsman2(self):
        return "..."
    
    ############# HOUND ARCHON 

    def gethoundarchon1(self):
        return "Red angels do not thrive in water."

    def gethoundarchon2(self):
        return "Do not venture too deep in the water.."

    ############# ANGEL 

    def getangel1(self):
        return "Try to find a good person and share with him."

    def getangel2(self):
        return "Celestia is watching you."

    ############# DEMON1 

    def getdemon11(self):
        return "Hissssss!"

    def getdemon12(self):
        return "Darkness will prevail ...!"

    def getangelusedspellbook(self):
        return "The book disappears in the angel's hands!"

    ############# CHERUBSTATUE1 

    def getcherubstatue11(self):
        return "May god bless you."

    def getcherubstatue12(self):
        return "Take the ring of the Seraphim next to me."

    def getcherubstatue13(self):
        return "May it aid you upon your quest."

    def getcherubstatue14(self):
        return "Watch out for the dark side of the lands."



# Copyright (C) Johan Ceuppens 2011
# Copyright (C) Johan Ceuppens 2010

# Copyright (C) Johan Ceuppens 2009 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


class Text(object):
    pass

#    def getname(self,o):
#        return o.name

# Copyright (C) Johan Ceuppens 2011
# Copyright (C) Johan Ceuppens 2010

# Copyright (C) Johan Ceuppens 2009 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.



class Utilities():
    def readandfindstring(self,string):
        f = open('./workfile', 'r')

        for line in f:
            if line == string:
                return 1
        return None
import pygame, sys,os
from pygame.locals import *



class Vector3():
    def __init__(self, xx,yy,zz):
	self.array = [xx,yy,zz]
	
    def multiply(self, v1):
	retv = Vector3(1.0,1.0,1.0) 
	for i in [0,1,2]:
	    retv.array[i] = self.array[i] * v1[i]
	return retv

    def dotp(self, v1):
	ret = 0.0
	for i in [0,1,2]:
	    ret += self.array[i] * v1[i]
	return ret

# Copyright (C) Johan Ceuppens 2009 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import pygame

#from Numeric import *
#import pygame.surfarray as surfarray

from pygame.locals import *
from maproom import *
from room import *
from roomroomman import *
from box import *

from scrollinvisibility import *

class DungeonundertreeCastle(Room):
    "Dungeon under castle of assassin queen's dungeon - north of triple crossroads"

    def __init__(self,x,y, roomman):
        Room.__init__(self)
	self.roomman = roomman
	self.player = self.roomman.player

    def loadroom(self):
        self.initroom()
        self.loadroom1()
        return self

    def loadroom1(self):

	factory = Entityfactory()

	# OUTER WALLS
	self.addbox(0, 0, 300, 100)
	self.addbox(400, 0, 100, 100)
	self.addbox(0, 0, 100, 480)
	self.addbox(540, 0, 100, 480)
	self.addbox(0, 380, 640, 100)

	# STATUES 
	### self.addbox(475, 75, 70, 70)
	
	# ITEMS 
        # ENTITIES
	# ENEMIES
        ###self.addentity(factory.make(Entity.ENTITY_CAPRICORNGUARDIAN3,300,110,50,50,self.boxes,self.roomman.player, self.entities))
        self.addenemy(factory.makewith(Entity.ENTITY_CAPRICORNGUARDIAN3,300,110,50,50,self.boxes,self.entities,self.player))
        self.addenemy(factory.makewith(Entity.ENTITY_CAPRICORNGUARDIAN3,350,110,50,50,self.boxes,self.entities,self.player))
	# EXITS
	# NORTH EXIT
        ###self.addexit(300,0,100,100,4008,300,380,DungeonundertreeCastle8(0,0, self.roomman))
	# SOUTH EXIT
        ###self.addexit(300,380,100,100,4002,300,125,DungeonundertreeCastle4(0,0, self.roomman))
        self.background = pygame.image.load('./pics/bg_dungeonundertreecastle7.bmp').convert()

class Room1world(Room):
    ""
    def __init__(self, roomman):
        Room.__init__(self)
	self.roomman = roomman

    def loadroom(self):
        self.initroom()
        self.loadworldroom1()
        return self


    def loadworldroom1(self):
        
        factory = Entityfactory()
        self.addfactory(factory)

#	item = Scrollinvisibility(300,220,50,50,"1","1")
#        self.addentity(item)
#        item.readkeys(self.entities)

#        self.addentity(factory.make(Entity.ENTITY_TREE2, 0,0,130,130))

#        self.addentity(factory.make(Entity.ENTITY_FOOD,120,350,40,40))

#        self.addenemy(factory.make(Entity.ENTITY_SKELETON2,400,390,50,50))     #         self.addenemy(factory.make(Entity.ENTITY_SKELETON2,450,390,50,50))
#        self.addenemy(factory.make(Entity.ENTITY_MYCONID,400,390,50,50))     
#         self.addenemy(factory.make(Entity.ENTITY_SKELETON2,450,390,50,50))

        # WALLS
        self.addbox(0,0,500,200)
        # NORTHEAST EXIT
        self.addexit(500,0,100,160,3003,310,230,Room2World(self.roomman))
        # WEST EXIT
        ###FIXself.addexit(0,200,60,280,3002,310,230,Room2World(self.roomman))

        # NORTH EXIT
#        self.addexit(0,0,640,80,3001,310,230,Roomforest1(0,-420, self.roomman))

        # DUNGEON 1 GATE
#        self.addexit(350,200,90,80,1.1,210,390,Room1dungeon1())

        # SOUTH EXIT
#        self.addexit(0,430,640,50,1002,210,90,Room2world(self.roomman))

        # EXITS
#        self.addexit(0,0,640,100,1,100,100)

        self.background = pygame.image.load('./pics/worldroom1.bmp').convert()


class Room2world(Maproom):
    ""
    def __init__(self, roomman):
        ###Maproom.__init__(self,-320,0)
        ### Maproom.__init__(self,-700,-700)##FIXME1
        Maproom.__init__(self,0,-300,font)##FIXME1
	self.roomman = roomman
	self.player = self.roomman.player
	###FIXaddonself.octtree = Octtree(500,500,500,300,300,300,1,self)
	self.octtree = Octtree(500,500,500,300,300,300,0,self)
	self.octtree.generatetree(100)
	self.trees.addobject(self.octtree)
	self.octtree = Octtree(1000,400,1000,300,300,300,1,self)
	self.octtree.generatetree(400)
	self.trees.addobject(self.octtree)

    def loadroom(self):
        self.initroom()
        self.loadworldroom2()
        return self


    def loadworldroom2(self):
       
        factory = Entityfactory()
        self.addfactory(factory)

#        self.addentity(factory.make(Entity.ENTITY_TREE2, 0,0,130,130))
#        self.addenemy(factory.make(Entity.ENTITY_MYCONID,400,390,50,50))     
        self.addenemy(factory.makewith(Entity.ENTITY_BOMBMAN1,450,90,333,333,self.boxes,self.entities,self.player))

        ###self.addblock(0,0,750,500,200,200)
        # WALLS
#        self.addbox(0,0,500,200)
        # NORTHEAST EXIT
#        self.addexit(580,0,100,400,3001,310,230,Room2World(0,0))
        # WEST EXIT
#        self.addexit(0,200,60,60,1.6,570,210)

        # NORTH EXIT
#        self.addexit(0,0,640,80,3001,310,230,Roomforest1(0,-420, self.roomman))

        # DUNGEON 1 GATE
#        self.addexit(350,200,90,80,1.1,210,390,Room1dungeon1())

        # SOUTH EXIT
#        self.addexit(0,430,640,50,1002,210,90,Room2world(self.roomman))

        # EXITS
#        self.addexit(0,0,640,100,1,100,100)

        self.background = pygame.image.load('./pics/bgrot1-1000x1000.bmp').convert()
        ###self.background = pygame.image.load('./pics/blank.bmp').convert()
\end{verbatim}
\end{mylisting}


\section{\large Conclusion}


\bibliographystyle{plain}
\bibliography{refs} % refs.bib

\end{document}

